Save activity state in android apps (for example when we rotate our device portrait to landscape or landscape to portrait the values which we had filled in EditText gone away)
You need to override
onSaveInstanceState(Bundle savedInstanceState)
and write the application state values you want to change to the
Bundle
parameter like this
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This b
undle will be pa
ssed to onCreate if the process is
// killed and restarted.
savedInstanceState.putBoolean("BooleanValueKey", true);
savedInstanceState.putDouble("DoubleValueKey", 1.9);
savedInstanceState.putInt("Int
ValueKey
", 1);
savedInstanceState.putString("String
ValueKey
", "Welcome to Android Code Portal");
// etc.
}
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:
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean booleanValue = savedInstanceState.getBoolean("Boolean
ValueKey
");
double double
Value
= savedInstanceState.getDouble("Double
ValueKey
");
int int
Value
= savedInstanceState.getInt("Int
ValueKey
");
String string
Value
= savedInstanceState.getString("String
ValueKey
");
}