Orientation Change in Android


By default, when the device orientation changes, Android destroys the active activity and creates a new one. This lets you load a different layout for the new orientation. Unfortunately, this also means, you will need to reload any data from the server or database. Prior to Android 3.0 (Honeycomb), Android provided a way to pass data from the old activity to the new one through the onRetainNonConfigurationInstance()/getLastNonConfigurationInstance() methods. This approach has since been deprecated. The Fragment API provides a better option. The process is still fraught with danger, if your fragment does any advanced work like background tasks and progress dialog.

In this article, we will explore options to deal with orientation change. For majority of the application, the choices can be surprisingly simple.

Option 0 – Do Nothing

As I said, if you do nothing, the activity will be destroyed after an orientation change. A new one will be created. This option is only valid if the initialization process for the activity (from onCreate()) is trivial and very quick. If onCreate() is time consuming or you start asynchronous tasks from there, things can become very slow and buggy when orientation changes. In those situations, consider the following options.

Option 1 – Fix Orientation

Many activities work best in one orientation. Other orientations may be harmful to user experience. Sample scenarios are:

  • A movie player should use the landscape orientation. Portrait orientation is just too narrow.
  • An activity showing a list of customers work best in portrait mode. Landscape mode shows fewer customers.

In situations like these, you should fix the orientation of the activity. You do that by setting the screenOrientation attribute in the manifest file.

<activity
android:label="Customer List"
android:name="CustomerListActivity"
android:screenOrientation="portrait">

</activity>

Option 2 – Recycle the Activity

For majority of applications, simply resizing the views to fit into the new orientation is sufficient. The layout managers are pretty good at resizing views. All you need to do is find a way to not destroy the activity after an orientation change and simply have the views resized. This can be easily done by setting the configChanges attribute. For example:

<activity
android:label="Address Book"
android:name=".MainActivity"
android:configChanges="orientation|screenSize">

</activity>

Note: As of Android 3.0, you will need to add the screenSize flag. This wasn’t necessary prior to that.

This option is most preferred if you have a complicated initialization process for an activity or fragment within an activity. For example, they can run an asynchronous task and show a progress dialog when the orientation changes. Everything will continue to function normally and uninterrupted.

Option 3 – Retain Fragment

This option is appropriate when these conditions exist:

  1. The application must show an orientation specific layout. To do this, the old activity has to be destroyed. The new activity will load a different layout. And,
  2. The initialization process is time consuming or does asynchronous work and should not be redone for the new activity.

This is the most complex scenario. Very few applications will fall into this category. So, choose this option carefully and if you must.

Starting from Android 3.0, the correct approach to handle this problem is through fragment. Instead of managing the views and initializing them directly from the activity, do those things from a fragment. When orientation changes, the activity instance is destroyed, but, the fragment object can be recycled. This is a two step process:

  1. Flag the fragment object as retainable (recyclable). After that, when orientation changes, the fragment is detached from the old activity and attached to the new one. Note: The views managed by the fragment get destroyed and the fragment must reload the layout from onCreateView.
  2. Save all state data in the member variables of the fragment object. Since, the fragment object survives an orientation change, all state data survives with it. State data includes and data loaded from web service or database, AsyncTask and ListAdapter. This gives you a lot of flexibility. For example, an AsyncTask started by the fragment can continue after an orientation change.

Flag the fragment instance as retainable from onCreate() of the fragment class.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

From the onCreate() view method, load a layout and recreate the view hierarchy as usual.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    super.onCreateView(inflater, container, savedInstanceState);
    View v = inflater.inflate(R.layout.addressbook, container);
    return v;
}

The onActivityCreated() is the complex one. It needs to load data only if it hasn’t been already loaded. It needs to populate the views in all cases. That’s because, the views are recreated after orientation change.

In the example below, the fragment uses a background task to load data. We start the task only if hasn’t been started yet. If the task has already finished, we simply populate the views.

public class CustomerListFragment extends Fragment {
CustomerListTask mTask;
ArrayList<Customer> mCustomerList;

public View onCreateView(…){…}

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

    if (mTask != null) {
        if (mTask.getStatus() == Status.FINISHED) {
        //Data is already loaded. Show it
        showCustomerList(mCustomerList);
    }

    //Task still running
    return;
    }

    //Start work in background
    mTask = new CustomerListTask();
    mTask.execute();
}

class CustomerListTask extends AsyncTask<Void, Void, ArrayList<Customer>> {

    protected ArrayList<Customer> doInBackground(Void... params) {
        mCustomerList = fetchCustomerList();
        return mCustomerList;
    }

    protected void onPostExecute(ArrayList<Customer> list) {
        showCustomerList(list);
    }
}

}
  1. #1 by Mike on August 28, 2012 - 11:41 am

    Just been updating my old android app (a japanese study app) to android 4.0 from 2.3 and i got the deprecated warnings coming through. Really glad I found this site, has clarified the steps I can take to resolve it.

    Thanks!

  2. #2 by Neil on September 4, 2012 - 5:40 am

    According to the documentation, This can only be used with fragments not in the back, any idea how to work around this?
    stackhttp://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)

  3. #3 by Jeff on October 25, 2012 - 12:33 pm

    Thank you for providing this clear and concise article regarding this very confusing portion of Android. Google needs to hire you if they haven’t already!

  4. #4 by Robert on October 26, 2012 - 10:11 am

    This was HUGE for me – I’ll be bookmarking for sure – thank you so much. Thought I was going to have to do a bunch of fragment saving state and all that – didn’t realize that I could skip it all if I just specify the configChanges – whew!

(will not be published)

*