Titanium Saving the state of the last window viewe

2019-08-30 18:46发布

问题:

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 up into separate files. Would I go about this by making a couple of global variables in the model, and calling them with app.properties? If you can help, could you please provide an example?

Thanks for any help <3

回答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.



回答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 {
            return null;
        }
    },


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 dimensions
    var 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 size
    var 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



回答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 open
var lastwin = Ti.App.Properties.getString("lastwin");

if ( lastwin == 'SavedList'){
    SavedList.open();
    SavedListNav.open();
}
else if (lastwin == 'tomList'){
    tomList.open();
    TomNav.open();
}
else if (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();
});