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.
There are basically two ways to implement this change.
onSaveInstanceState()
andonRestoreInstanceState()
.android:configChanges="orientation|screenSize"
.I really do not recommend to use second method. Since in one of my experience it was causing half of the device screen black while rotating from portrait to landscape and vice versa.
Using first method mentioned above , we can persist data when orientation is changed or any config change happens. I know a way in which you can store any type of data inside savedInstance state object.
Example: Consider a case if you want to persist Json object. create a model class with getters and setters .
Now in your activity in onCreate and onSaveInstanceState method do the following. It will look something like this:
There is a way to make Android save the states without implementing any method. Just add this line to your Manifest in Activity declaration:
It should look like this:
Here you can find more information about this property.
It's recommended to let Android handle this for you than the manually handling.
onSaveInstanceState
is called when the system needs memory and kills an application. It is not called when the user just closes the application. So I think application state should also be saved inonPause
It should be saved to some persistent storage likePreferences
orSqlite
Instead of that, you should use ViewModel, which will retain the data until the activity life cycle.
When an activity is created it's onCreate() method is called.
savedInstanceState is an object of Bundle class which is null for the first time, but it contains values when it is recreated. To save Activity's state you have to override onSaveInstanceState().
put your values in "outState" Bundle object like outState.putString("key","Welcome Back") and save by calling super. When activity will be destroyed it's state get saved in Bundle object and can be restored after recreation in onCreate() or onRestoreInstanceState(). Bundle received in onCreate() and onRestoreInstanceState() are same.
or
To get activity state data stored in
onCreate()
, first you have to save data in savedInstanceState by overridingSaveInstanceState(Bundle savedInstanceState)
method.When activity destroy
SaveInstanceState(Bundle savedInstanceState)
method gets called and there you save data you want to save. And you get same inonCreate()
when activity restart.(savedInstanceState wont be null since you have saved some data in it before activity get destroyed)