I am using onConfigurationChanged()
. In that, when I am changing from LandScape to Portrait, it is calling if (newConfig.orientation == Configuration.ORIE
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).
Write this line in the android Manifest.xml file:
<activity
android:name="MyActivity"
android:configChanges="orientation|keyboardHidden" />
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.
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"
/>
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
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.