I've encountered a problem wile using my own subclass of android.app.Application. I have numerous activities in my application and I'm running a custom ROM which consumes quite a lot of memory. What happens is when I launch a third party activity (Camera) my Application is killed. My Application subclass stores vital data for the whole app so it can't work without it. android.app.Application doesn't have any methods for saving or restoring application state. Activity does have them but they are not suitable.
Any guesses how to perform save/restore state on Application subclass?
UPDATE I've managed to do so by filling a Bundle obtained from o开发者_如何学运维nSaveInstanceState and restoring values in onCreate. But is there any better way?
According to http://developer.android.com/guide/topics/fundamentals.html
in reference to onCreate()
Called when the activity is first created. This is where you should do all of your normal static set up — create views, bind data to lists, and so on. This method is passed a Bundle object containing the activity's previous state, if that state was captured (see Saving Activity State, later). Always followed by onStart().
in reference to onPause()
Called when the system is about to start resuming another activity. This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on. It should do whatever it does very quickly, because the next activity will not be resumed until it returns. Followed either by onResume() if the activity returns back to the front, or by onStop() if it becomes invisible to the user.
Using the Bundle
objects are the way you are supposed to save your state in the event that the application is killed. The onPause()
method is supposed to be a signal to your application that it is now in a state where it could be killed to reclaim resources. The onCreate()
method is passed a bundle so that it can pick up where the application previously left off as to provide a seamless user experience while still allowing the system to kill the application when it needs resources.
精彩评论