Skip to content Skip to sidebar Skip to footer

Titanium Saving The State Of The Last Window Viewed

How would i go about saving the state of the last viewed window of my application? I have been trying titanium.app.properties with no luck. I am using MVC so everything is split u

Solution 1:

  1. when the app launches i.e with $.index.open() save a property i.e Ti.App.Properties.setString("lastwin",nameofwindow)
  2. Whenever you open any other window from this window save the name of the window in the String.
  3. in the focus event of each window also update the name in the string.something like :

    window.addEventListener("focus",function(){

    Ti.App.Properties.setString("lastwin",window);
    

    });

Hope it helps.

Solution 2:

The Pageflow Widget has an example of how you can handle this.

There is a global variable that holds all the pages that were initialized and pushes them onto an array, like a stack.

var newPageView = newPage.getView();
pageflow.pages.push(newPage);

It then has a back handler:

getPreviousPage: function() {
        if (pageflow.pages.length >= 2) {
            return pageflow.getPage(pageflow.getCurrentPageId() - 1);
        } else {
            returnnull;
        }
    },


back: function() {
        var previousPage = pageflow.getPreviousPage();
    if (previousPage) {
        var currentPosition = pageflow.getGridCoordinatesForPage(pageflow.getCurrentPageId() - 1);

        $.pageflow.animate({ left: currentPosition.left, top: currentPosition.top, duration: 300 }, function() {
            $.pageflow.left = currentPosition.left;
            $.pageflow.top = currentPosition.top;
            pageflow.removeLastPage(true, true);
        });
    }
},
removeLastPage: function(callPrePostHide, callPrePostShow) {
        var remove = pageflow.pages.pop();
        remove.removeEventListeners();
    if (callPrePostHide) {
        remove.preHide();
    }

    var removeView = pageflow.pagesViews.pop();
    $.pageflow.remove(removeView);
    pageflow.pagesGridPositions.pop();

    if (callPrePostHide) {
        remove.postHide();
    }

    var currentPage = pageflow.getCurrentPage();

    if (callPrePostShow && currentPage) {
        currentPage.preShow();
    }

    // move the grid to adapt its new dimensionsvar currentPageId = pageflow.getCurrentPageId();
    var currentPosition = pageflow.getGridCoordinatesForPage(currentPageId);
    $.pageflow.top = currentPosition.top;
    $.pageflow.left = currentPosition.left;

    // move all the page views
    _.each(pageflow.pagesViews, pageflow.fixPagePosition);

    // fix grid sizevar gridDimensions = pageflow.getGridDimensions();
    $.pageflow.width = gridDimensions.width;
    $.pageflow.height = gridDimensions.height;

    if (callPrePostShow && currentPage){
        currentPage.postShow();
    }
},

You can see the complete working code by downloading and running the widget. This code does a lot more, but you can see how their back function works and remembers the previous screen.

https://github.com/jolicode/Badass-Pageflow

Solution 3:

So i have been able to get it to work by simply doing as stated in another answer. for anyone interested It goes as follows

//This decides what screen it should openvar lastwin = Ti.App.Properties.getString("lastwin");

if ( lastwin == 'SavedList'){
    SavedList.open();
    SavedListNav.open();
}
elseif (lastwin == 'tomList'){
    tomList.open();
    TomNav.open();
}
elseif (lastwin == 'Recipes'){
    Recipes.open();
    RecipesNav.open();
}
else MainMenu.open();

//in each function that leads to the specific screen you want do it like so
SavedListsBTN.addEventListener('click', function(e){
    Ti.App.Properties.setString("lastwin", 'SavedList');
    SavedList.open();
    SavedListNav.open();
    MainMenuNav.close();
    MainMenu.close();
});

Post a Comment for "Titanium Saving The State Of The Last Window Viewed"