Handling Configuration Changes in Android

There are various situations such as when the screen orientation is rotated where the Activity can actually be destroyed and removed from memory and then re-created from scratch again. In these situations, the best practice is to prepare for cases where the Activity is re-created by properly saving and restoring the state.

Saving and Restoring Activity State

If a user navigates to a different away from the activity, the onPause() and onResume() methods are called. If you need to retain state information in those cases, it's best to save state through the use of Shared Preferences:

public class MainActivity extends Activity { static final String SOME_VALUE = "int_value"; static final String SOME_OTHER_VALUE = "string_value"; int someIntValue; String someStringValue; @Override protected void onPause() { SharedPreferences settings = getSharedPreferences("Settings", Context.MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit(); editor.putInt(SOME_VALUE, someIntValue); editor.putString(SOME_OTHER_VALUE, someStringValue); editor.apply(); } }

Upon resume, the onResume() gets called:

@Override public void onResume() { SharedPreferences settings = getSharedPreferences("Settings", Context.MODE_PRIVATE); someIntValue = settings.getInt(SOME_VALUE, 0); }

Note that onSaveInstanceState() is called right before your activity is about to be killed or restarted because of memory pressure or screen orientation. This is different from onPause() which gets called when your activity loses focus (i.e. you transition to another activity). The default implementation of this method automatically saves information about the state of the activity's view hierarchy, such as the text in an EditText widget or the scroll position of a ListView. For other data to persist, you can put the data in the Bundle provided.

public class MainActivity extends Activity { static final String SOME_VALUE = "int_value"; static final String SOME_OTHER_VALUE = "string_value"; @Override protected void onSaveInstanceState(Bundle savedInstanceState) { // Save custom values into the bundle savedInstanceState.putInt(SOME_VALUE, someIntValue); savedInstanceState.putString(SOME_OTHER_VALUE, someStringValue); // Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(savedInstanceState); } }

The system will call that method before an Activity is destroyed. Then later the system will call onRestoreInstanceState where we can restore state from the bundle:

Override protected void onRestoreInstanceState(Bundle savedInstanceState) { // Always call the superclass so it can restore the view hierarchy super.onRestoreInstanceState(savedInstanceState); // Restore state members from saved instance someIntValue = savedInstanceState.getInt(SOME_VALUE); someStringValue = savedInstanceState.getString(SOME_OTHER_VALUE); }

Instance state can also be restored in the standard Activity#onCreate method but it is convenient to do it in onRestoreInstanceState which ensures all of the initialization has been done and allows subclasses to decide whether to use the default implementation. Read this stackoverflow post for details.

Note that onSaveInstanceState and onRestoreInstanceState are not guaranteed to be called together. Android invokes onSaveInstanceState() when there's a chance the activity might be destroyed. However, there are cases where onSaveInstanceState is called but the activity is not destroyed and as a result onRestoreInstanceState is not invoked.

Share:
30 Comments
  • October 19,2018

    There are many variations of passages of Lorem Ipsum available, but the majority have alteration in some form, by injected humour.

    • October 19,2018

      There are many variations of passages of Lorem Ipsum available, but the majority have alteration in some form, by injected humour.

  • October 19,2018

    There are many variations of passages of Lorem Ipsum available, but the majority have alteration in some form, by injected humour.

Write a comment
Your email address will not be published. Required fields are marked *
Review*
Name*
Email*
Notify me of new posts by email