Skip to content

Handling Configuration Changes (or not)

December 27, 2009

Hey everyone,

There’s a lot of stuff out there on how to do complicated handling of configuration changes (i.e. overriding the onConfigurationChange method in the Activity, manually reconstructing your views from there, etc), but my question was:

“What if I don’t want to do anything in my activity when the configuration changes?”

Even if you add the:

<activity android:name=".Activity" android:label="@string/app_name" android:screenOrientation="portrait" />

screenOrientation clause into your Android Manifest, when you change the orientation your Activity still restarts – it simply forces itself to either show up in the portrait orientation or in the landscape orientation. Now, this not only can cause the user to experience a little “hiccup” in their experience (namely, that the Activity may appear to blink because of the restart), but in my instance, I had threads running in the background that were performing calculations and by changing the orientation and causing the Activity to restart this was causing all sorts of nasty threading issues. So the solution?

To prevent the Activity from restarting itself here’s what you have to do:

In your Android Manifest, add this to your activity:

<activity android:name=".Activity" android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation" />

which basically tells your application that you’ll handle any orientation or keyboard slides by yourself. Then in your activity, all you need to do is override the onConfigurationChange method, and that’s it!

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

So now, you tell your application that you’ll handle the configuration changes, and when you handle them you simply do nothing!

Happy coding.

- jwei

About these ads
4 Comments leave one →
  1. pramod deore permalink
    August 15, 2010 8:52 pm

    I had tried it but , still if screen orientation changes my activity restarted. How to prevent activity from restarting?

    • August 26, 2010 7:12 pm

      This should work… check your Android Manifest and make sure all your parameters are in there. If you follow these steps I can’t think of a case where it wouldn’t work.

  2. March 2, 2011 1:26 am

    The problem with the post is that the code:

    android:screenOrientation=”portrait”

    makes the configuration handler pointless. What you are doing is solving the same problem twice and confusing the solutions, right?

  3. Nitesh permalink
    April 11, 2011 8:29 pm

    Comment super.onConfigurationChanged(newConfig);

    //super.onConfigurationChanged(newConfig);

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Follow

Get every new post delivered to your Inbox.

Join 876 other followers

%d bloggers like this: