The Activity class provides two methods onSaveInstanceState() and onRestoreInstanceState(), for saving and restoring activity state. The onSaveInstanceState( ) method is a callback method that is used to save the application state.

@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("key1", true);
  savedInstanceState.putDouble("key2", 1.9);
  savedInstanceState.putInt("key3", 1);
  savedInstanceState.putString("key4", "Welcome back to Android");
}

The Bundle is essentially a way of storing a Key value map. And it will get passed in to onCreate() and also onRestoreInstanceState() where you can extract the values as follows.

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  boolean myBoolean = savedInstanceState.getBoolean("key1");
  double myDouble = savedInstanceState.getDouble("key2");
  int myInt = savedInstanceState.getInt("key3");
  String myString = savedInstanceState.getString("key4");
}
Tagged in : #Android

Discussions

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.