Ionic hybrid app, saving application state/data on exit

南笙酒味 提交于 2019-12-08 03:18:54

问题


I'm building an app using the ionic framework with angular js, and I'm a little confused on what the best way is to go about saving and restoring the users state when they exit the app. For example, in iOS if the user manually terminates the app, or if the phone is turned off for some reason, I want the user to be able to open my app up and still be at the same place and see the same data, even if they are offline.

I have been using HTML localstorage, and understand how to save and restore the users data locally, and similarly I understand how to save a reference to their current state. When the user is actually using the app however I'm gonna need to be pulling and pushing data from a cloud database, so I would only really use the local storage to save the users state when they exit.

What is the best way to go about saving their state in angular? When should I do it, and is their a way to tell if and when the app is terminating. Ideally I would like to be able to call a function to save the state right before the app terminates, however I have been unable to find the right way to do this in angular.

As this is a fairly specific issue, Im hoping to learn to right way to do it. Thanks!!

What Im looking for

// P S U D O    C O D E 
on('application_exit', function(){
    // save user data
})

回答1:


You can use the Cordova "pause" event, which is triggered whenever your app is sent to the background.

document.addEventListener("pause", yourCallbackFunction, false);

http://docs.phonegap.com/en/2.9.0/cordova_events_events.md.html#pause

You'll want to look at the iOS quirks to make sure they fit your use case, however.



来源:https://stackoverflow.com/questions/32031153/ionic-hybrid-app-saving-application-state-data-on-exit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!