0

I have an app that uses the below sdk in the manifest:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

To prevent the activity from restarting on screen rotation I add to manifest :

 android:configChanges="orientation|keyboardHidden|screenSize"

Do I need to override the onConfigurationChanged in activity itself also, as below:

   @Override
 public void onConfigurationChanged(Configuration newConfig) {
 super.onConfigurationChanged(newConfig);
 setContentView(R.layout.myLayout);
 }

or is this line in the manifest is enough:

  android:configChanges="orientation|keyboardHidden|screenSize"

Any help will be appreciated.

keyser
  • 18,829
  • 16
  • 59
  • 101
androidqq6
  • 1,526
  • 2
  • 22
  • 47

3 Answers3

2

To prevent the activity from restarting on screen rotation

That is usually not a very good idea.

I add to manifest : android:configChanges="orientation|keyboardHidden|screenSize"

That is usually even less of a good idea. All you do is create more work for yourself. You still have to have all of the standard configuration change logic, to handle all of the configuration changes that you do not have listed there, plus you may need to deal with these three cases separately.

Do I need to override the onConfigurationChanged

That depends upon whether your UI is different based upon configuration, for the configurations you have listed in android:configChanges (e.g., does portrait look different than landscape?). If the answer is "yes", then onConfigurationChanged() is where you will fix up that UI for the events listed in your android:configChanges roster. If the answer is "no", then you do not need to override this method.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

The manifest piece below that you had is enough to prevent the rotation:

android:configChanges="orientation|keyboardHidden|screenSize"
TronicZomB
  • 8,667
  • 7
  • 35
  • 50
  • just to know what the benefit of using the code of override the onConfigurationChanged in activity or when we need to use it . – androidqq6 Jul 24 '13 at 23:41
  • I can't remember at the moment and can't find the resource, but I do know that Google actually tries to shy away from this method and calls it more of a hack than a method and that onSavedInstanceState, etc should be used when possible. I will try to get back to you on more specifics later. – TronicZomB Jul 24 '13 at 23:44
  • @TronicZomB Nice, we'll be here waiting – Joaquin Iurchuk Dec 22 '15 at 21:19
-2

Set

android:screenOrientation="portrait"

as suggested by Android - disable landscape mode?

Community
  • 1
  • 1
andy256
  • 2,821
  • 2
  • 13
  • 19