I've been working on the 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:
package com.android.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
private TextView mTextView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextView = new TextView(this);
if (savedInstanceState == null) {
mTextView.setText("Welcome to HelloAndroid!");
} else {
mTextView.setText("Welcome back.");
}
setContentView(mTextView);
}
}
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.
Here is a comment from Steve Moseley's answer (by ToolmakerSteve) that puts things into perspective (in the whole onSaveInstanceState vs onPause, east cost vs west cost saga)
Note that it is NOT safe to use
onSaveInstanceState
andonRestoreInstanceState
for persistent data, according to the documentation on Activity states in http://developer.android.com/reference/android/app/Activity.html.The document states (in the 'Activity Lifecycle' section):
In other words, put your save/restore code for persistent data in
onPause()
andonResume()
!EDIT: For further clarification, here's the
onSaveInstanceState()
documentation:Saving state is a kludge at best as far as I'm concerned. If you need to save persistent data, just use an SQLite database. Android makes it SOOO easy.
Something like this:
A simple call after that
I think I found the answer. Let me tell what I have done in simple words:
Suppose I have two activities, activity1 and activity2 and I am navigating from activity1 to activity2 (I have done some works in activity2) and again back to activity 1 by clicking on a button in activity1. Now at this stage I wanted to go back to activity2 and I want to see my activity2 in the same condition when I last left activity2.
For the above scenario what I have done is that in the manifest I made some changes like this:
And in the activity1 on the button click event I have done like this:
And in activity2 on button click event I have done like this:
Now what will happen is that whatever the changes we have made in the activity2 will not be lost, and we can view activity2 in the same state as we left previously.
I believe this is the answer and this works fine for me. Correct me if I am wrong.
The
onSaveInstanceState(bundle)
andonRestoreInstanceState(bundle)
methods are useful for data persistence merely while rotating the screen (orientation change).They are not even good while switching between applications (since the
onSaveInstanceState()
method is called butonCreate(bundle)
andonRestoreInstanceState(bundle)
is not invoked again.For more persistence use shared preferences. read this article
My problem was that I needed persistence only during the application lifetime (i.e. a single execution including starting other sub-activities within the same app and rotating the device etc). I tried various combinations of the above answers but did not get what I wanted in all situations. In the end what worked for me was to obtain a reference to the savedInstanceState during onCreate:
and use that to obtain the contents of my variable when I needed it, along the lines of:
I use
onSaveInstanceState
andonRestoreInstanceState
as suggested above but I guess i could also or alternatively use my method to save the variable when it changes (e.g. usingputBoolean
)