Saving Android Application State

I understand how to save the state of an application using SharedPreferences, onSavedInstanceState () and onRestoreInstanceState (), etc., as described in a similar entry ( Saving Android activity state using Save State State State ), but how to save the last action?

To be more specific, my application launches and goes to the login screen. As soon as the user logs into the system and moves through several actions, let's say that he or she leaves the application using the home button or in some other way. The next time the user starts the application, he will return to the login screen and log back in. Instead, I want the application to start and go to the last action, which was on top of the stack when the user left the application in a previous session.

How is the last action saved so that it can be restored when the application starts?

+4
source share
3 answers

I believe that Android does this automatically. We have an application that we are working on. When I press the home button and they return to the application, it starts with the activity that I stopped at. We have not written any code for this to happen. This seems to work.

+2
source

My colleague wrote an article about the state of an Android application, including information about getLastNonConfigurationInstance (), which retrieves the last instance that was saved. Have a look here: http://www.eigo.co.uk/Managing-State-in-an-Android-Activity.aspx

+2
source

Please note that the answer above is true only in one case: if your process is not killed by android, because resources (memory, ...) are needed for another reason.

To get what you described, I would write custom parental activity and override the correct lifecycle methods to save your application state, read it, and act accordingly. Then let all your actions inherit from MyActivity (instead of android.app.Activity)

public MyActivity extends android.app.Activity { ... @Override public onCreate(...) { // Read the Application state, // check if the currently launching Activity is the right one, // if not, start the last Activity and finish the current one. } @Override public onDestroy(...) { // Store the current Activity ID in the SharedPreferences } ... } 

Take care of the calls to the super.onDestroy and super.onCreate methods in all of your assets (as in any case).

Happy coding and have fun with Android!

+1
source

All Articles