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
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 bundle will be passed to onCreate if the process is // killed and restarted. savedInstanceState.putBoolean("BooleanValueKey", true); savedInstanceState.putDouble("DoubleValueKey", 1.9); savedInstanceState.putInt("IntValueKey", 1); savedInstanceState.putString("StringValueKey", "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("BooleanValueKey"); double doubleValue= savedInstanceState.getDouble("DoubleValueKey"); int intValue= savedInstanceState.getInt("IntValueKey"); String stringValue= savedInstanceState.getString("StringValueKey"); }
No comments:
Post a Comment