tags:

views:

537

answers:

3

I understand how to save an application's state by using SharedPreferences, onSavedInstanceState() & onRestoreInstanceState(), etc as outlined in a similar post ( http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state ), but how do I save the last activity?

To be more specific, my application starts up and goes to a login screen. Once a user logs in and navigates through several activities, lets say he or she leaves the app using the home button or in some other way. Next time the user starts the app, it will go back to the login screen and do a login again. Instead, I want the app to start up and go to the last activity that was on top of the stack when the user left the app in the previous session.

How is the last activity saved so that it can be restored on app startup?

A: 

I believe Android does this automatically. We have an app that we are working on. When I click the home button and them come back to the app, it starts in the activity where I left off. We have not written any code to make this happen. It just seems to work.

Jay Askren
I think you're right Jay,I looked at my manifest file and noticed I had the following under my Main activity:<category android:name="android.intent.category.LAUNCHER" />Documentations of this attribute says: "The activity can be the initial activity of a task and is listed in the top-level application launcher. " Removing this line did the trick, thanks!
But if you remove the `LAUNCHER` category, won't you lose the ability to, er, launch your app? :)
Christopher
+2  A: 

Please note that the above answer is correct only on one case: If your process does not get killed by android because the resources (memory, ...) are needed for a different reason.

To get what you describe, I would write a custom parent activity and override the correct life-cycle methods to store your application state and read it and act accordingly. Then let all your activities 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 to call the super.onDestroy and super.onCreate methods in all your Activites (like you should do anyways).

Happy coding and have fun with Android!

Valentin
But surely the state to be saved will be different in each Activity you write, so there's no need for a custom parent class? Also, you should just use the techniques shown in the question jylc08 linked to, as `onDestroy` only gets called when you `finish()` an Activity (or the system kills it). Likewise `onCreate` is only called if the Activity isn't already in the task's stack.
Christopher
+1  A: 

My colleague has written an article on Android application state including details on getLastNonConfigurationInstance() which retrieves the instance that was last stored. Take a look here: http://www.eigo.co.uk/Managing-State-in-an-Android-Activity.aspx

Eigo