how i can stop the restarting or recalling of on create() on screen orientation ,i want to stop the recreation of activity on screen orientation. thanks in advance please te
Up to API 13
there was a new value to the configChanges
attribute, screenSize
So if you're using large screens make sure to add screenSize in your configChanges attribute:
android:configChanges="orientation|keyboardHidden|screenSize"
This is happening because when screen orientation rotates the Activity gets re-started. In this case you can add configChanges
attribute in your tag in the AndroidManifest file to stop the re-creation of the Activity.
<activity android:name=".Activity_name"
android:configChanges="orientation|keyboardHidden">
By, this also it won't stop though the orientation changes.
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
setContentView(R.layout.login_landscape);
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.login);
}
}
In your AndroidManifest.xml file, in the activity add
android:configChanges="keyboardHidden|orientation"
Example as below:
<activity android:name=".YourActivity" android:label="@string/app_name" android:configChanges="keyboardHidden|orientation">
Two ways for this:
Either you can set android:configChanges="keyboardHidden|orientation" in Manifest file to avoid recreation of activity.
Or Make the changes you want to apply on changing the orientation inside the overrided method
@Override
public void onConfigurationChanged(Configuration newConfig) {
// Perform the actions
super.onConfigurationChanged(newConfig);
}
Not the best, but maybe the easiest solution is to add
android:configChanges="keyboardHidden|orientation"
to youractivity in your manifest so it looks like
<activity android:name="com.your.activity"
android:configChanges="keyboardHidden|orientation"/>
There are a more full list of parameters for prevent activity recreations (in Manifest.xml):
<activity
android:name = ".MyActivity"
android:configChanges = "orientation|keyboard|keyboardHidden|screenLayout|screenSize">
</activity>