Every android activity follow a life-cycle. There are certain situation when an activity goes into pause state or in stopped state but at some particular time your application need to keep valuable state (i.e. data)
This we can achieve by using the callback method known as onSaveInstanceState( ) as follows:-
Here is the code snippet for storing the activity state:-
Step 1:- to store the values:-
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
/***This bundle will be passed to onCreate if the process is killed and restarted.***/
savedInstanceState.putBoolean("booleanValue", true);
savedInstanceState.putDouble("doubleValue", 1.9);
savedInstanceState.putInt("integerValue", 1);
savedInstanceState.putString("stringValue", "Getting Back");
}
Step 2:- to get the stored values:-
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// This bundle has also been passed to onCreate.
boolean storedBooleanValue = savedInstanceState.getBoolean("booleanValue");
double storedDoubleValue = savedInstanceState.getDouble("doubleValue");
int storedIntegerValue = savedInstanceState.getInt("integerValue");
String storedStringValue = savedInstanceState.getString("stringValue");
}
0 Comment(s)