How to use onConfigurationChanged() and newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE in android 2.3.3

后端 未结 6 1752
既然无缘
既然无缘 2020-11-27 05:28

I am using onConfigurationChanged(). In that, when I am changing from LandScape to Portrait, it is calling if (newConfig.orientation == Configuration.ORIE

相关标签:
6条回答
  • 2020-11-27 05:40

    you should also be aware of:

    Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

    0 讨论(0)
  • 2020-11-27 05:40

    Write this line in the android Manifest.xml file:

    <activity
      android:name="MyActivity"
      android:configChanges="orientation|keyboardHidden" />
    
    0 讨论(0)
  • 2020-11-27 05:49

    The issue is with the android:configChanges="orientation|keyboardHidden" .

    if you remove |keyboardHidden from the androidmanifest.xml, then the onConfigurationChanged is only fired when you rotate from landscape to portrait, not when you go from portrait to landscape (at least in the default emulator).

    Hope this helps.

    0 讨论(0)
  • 2020-11-27 05:50

    Write below code in your activity file.

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
         Log.e("On Config Change","LANDSCAPE");
        }
        else{
         Log.e("On Config Change","PORTRAIT");
        }
    
    }
    

    Also write below code in your manifest file as follows:

    <activity
            android:name=".activities.TestActivity"
            android:configChanges="orientation|screenSize|keyboardHidden"
    />
    
    0 讨论(0)
  • 2020-11-27 06:04

    Just write the below code into onConfigurationChanged method and test

    if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
    
        Log.e("On Config Change","LANDSCAPE");
    }else{
    
        Log.e("On Config Change","PORTRAIT");
    }
    

    and write the android:configChanges="keyboardHidden|orientation" into your manifiest file like this

    <activity android:name="TestActivity"
               android:configChanges="keyboardHidden|orientation">
    

    it's working at my side, i hope it helps you.

    If you're on tablet also add |screenSize to android:configChanges

    0 讨论(0)
  • 2020-11-27 06:04

    In addition to above answers: If you override the onConfigurationChanged method in combination with the updating of the manifest file you are saying you are handling the configurationchanges yourself. Therefore adding:

    setContentView(R.layout.layout.xml);
    

    In your onConfigurationChanged method - where layout is your layout file - will fix the problem.

    0 讨论(0)
提交回复
热议问题