Saving Android Activity state using Save Instance State

I’ve been working on Android SDK platform, and it is a little unclear how to save an application’s state. So given this minor re-tooling of the ‘Hello, Android’ example:

I thought it would be enough for the simplest case, but it always responds with the first message, no matter how I navigate away from the app.

I’m sure the solution is as simple as overriding onPause or something like that, but I’ve been poking away in the documentation for 30 minutes or so and haven’t found anything obvious.


You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter like this:

The Bundle is essentially a way of storing a NVP (“Name-Value Pair”) map, and it will get passed in to onCreate() and also onRestoreInstanceState() where you’d extract the values like this:

You would usually use this technique to store instance values for your application (selections, unsaved text, etc.).