Android Application Drawer: dealing with configuration changes

We have gone a long way with the Application Drawer pattern on Android but there is always stuff to cover. On the previous posts we covered how to create a very simple and and then a more complicated one.

This time I want to show you how to deal with configuration changes and the application drawer.

A “configuration change” is an event that will destroy and recreate the Activity object. This means the views and variables of this object will be cleared. Following the samples we have been discussing, this means your Application bar state will be affected since we change how it behaves depending on the type of fragment we are in (a root fragment with the hamburger, or a sub section fragment that uses a back arrow instead).

I will not even discuss the android:configChanges=”keyboardHidden|orientation|screenSize” since this will not protect you from other events that may trigger a configuration change. The correct thing to do is to deal with the changes and not try to avoid the event. There are two ways to work with this:

  1. Save the current state of the activity before it get’s destroyed and then restore it when it gets recreated.
  2. Use a helper object that is “retained” and it’s life is not attached to the Activity’s regular life cicle.

We will go with the first option for this tutorial although I will write about the second one in the future. Is up to you if you wan’t to investigate more about the second option from here.

As we learned, when a configuration changes the application get’s recreated. We will store the state before destroying the app. This is achieved by overriding the method onSaveInstanceState that will be called before your Activity gets destroyed.

protected void onSaveInstanceState(Bundle outState) {
the app will be recreated and the current toolbar will be null. It will be recreated
when the onCreate method is called. Store the current state to rebuild the toolbar correctly
AppBarState currentApbAppBarState = AppBarState.USE_DRAWER;
MyAppFragment currentFragment = getCurrentFragment();
if (currentFragment != null) {
currentApbAppBarState = currentFragment.getIsRootSection() ? AppBarState.USE_DRAWER : AppBarState.USE_BACK_NAVIGATION;
// store this value on the state
outState.putSerializable(INSTANCE_TOOLBAR_USE_DRAWER, currentApbAppBarState);

I have created an Enum to avoid using a plain boolean. The code is quite easy to understand. Now we have to deal with the onCreate method:

if (savedInstanceState == null) {

app should start on the home section
notice we won't be adding it to the back stack. Pressing back
should exist the app
navigateToSection(AppSection.INBOX, false);
} else {
the app has been recreated, we can obtain the saved state and restore the looks of the toolbar
AppBarState savedAppBarState = (AppBarState) savedInstanceState.getSerializable(INSTANCE_TOOLBAR_USE_DRAWER);
if (savedAppBarState == AppBarState.USE_DRAWER)


This is just a very simple tutorial that shows you the bases about how to store your Activity’s state. The next step is to see how individual Fragment’s can retain their values. Hopefully I will be writing about this soon. For now, you can browse the sample app for this solution from here:


1 thought on “Android Application Drawer: dealing with configuration changes”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s