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.
Kotlin code:
save:
and then in
onCreate()
oronRestoreInstanceState()
Add default values if you don't want to have Optionals
The
savedInstanceState
is only for saving state associated with a current instance of an Activity, for example current navigation or selection info, so that if Android destroys and recreates an Activity, it can come back as it was before. See the documentation foronCreate
andonSaveInstanceState
For more long lived state, consider using a SQLite database, a file, or preferences. See Saving Persistent State.
onSaveInstanceState()
for transient data (restored inonCreate()
/onRestoreInstanceState()
),onPause()
for persistent data (restored inonResume()
). From Android technical resources:Really
onSaveInstanceState()
is called when the Activity goes to background.Quote from the docs: "This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state." Source
What to save and what not to?
Ever wondered why the text in the
EditText
gets saved automatically while an orientation change? Well, this answer is for you.When an instance of an Activity gets destroyed and the System recreates a new instance (for example, configuration change). It tries to recreate it using a set of saved data of old Activity State (instance state).
Instance state is a collection of key-value pairs stored in a
Bundle
object.EditText
ListView
, etc.If you need another variable to be saved as a part of instance state you should OVERRIDE
onSavedInstanceState(Bundle savedinstaneState)
method.For example,
int currentScore
in a GameActivityMore detail about the onSavedInstanceState(Bundle savedinstaneState) while saving data
Which to choose for restoring Activity state?
OR
Both methods get the same Bundle object, so it does not really matter where you write your restoring logic. The only difference is that in
onCreate(Bundle savedInstanceState)
method you will have to give a null check while it is not needed in the latter case. Other answers have already code snippets. You can refer them.More detail about the onRestoreInstanceState(Bundle savedinstaneState)
Bonus
The
onSaveInstanceState(Bundle savedInstanceState)
is invoked by the system only when the user intends to come back to the Activity. For example, you are using App X and suddenly you get a call. You move to the caller app and come back to the app X. In this case theonSaveInstanceState(Bundle savedInstanceState)
method will be invoked.But consider this if a user presses the back button. It is assumed that the user does not intend to come back to the Activity, hence in this case
onSaveInstanceState(Bundle savedInstanceState)
will not be invoked by the system. Point being you should consider all the scenarios while saving data.Relevant links:
Demo on default behavior
Android Official Documentation.
you can use the
Live Data
andView Model
For Lifecycle Handel
FromJetPack
. see this Reference :https://developer.android.com/topic/libraries/architecture/livedata