I simply need nothing to change when the screen is rotated. My app displays a random image when it first loads and rotating the device should not select another random image. How can I (simply) make this behavior stop?
-
1It's also possible to lock (and possibly unlock) the screen orientation from code. Just call `Screen.lockOrientation(this)` from https://github.com/delight-im/Android-BaseLib/blob/master/Source/src/im/delight/android/baselib/Screen.java for this. It allows unlocking which the manifest does not. – caw Mar 04 '15 at 18:34
-
@Escobar Ceaser What is the proper solution for this question.Even I have same requirement. – Prabs Aug 03 '15 at 10:30
-
I would like to do this programmatically in code. – pollaris Sep 27 '17 at 16:05
17 Answers
There are generally three ways to do this:
As some of the answers suggested, you could distinguish the cases of your activity being created for the first time and being restored from
savedInstanceState
. This is done by overridingonSaveInstanceState
and checking the parameter ofonCreate
.You could lock the activity in one orientation by adding
android:screenOrientation="portrait"
(or"landscape"
) to<activity>
in your manifest.You could tell the system that you meant to handle screen changes for yourself by specifying
android:configChanges="orientation|screenSize"
in the<activity>
tag. This way the activity will not be recreated, but will receive a callback instead (which you can ignore as it's not useful for you).
Personally I'd go with (3). Of course if locking the app to one of the orientations is fine with you, you can also go with (2).
-
13So for #3 are you saying all I have to do is add that line to my manifest? Because it doesn't work. – Jimmy D May 06 '11 at 15:23
-
ps... #2 doesnt work either. it just forces the app to display in a certain orientation. rotating the screen still reloads the activity. – Jimmy D May 06 '11 at 15:42
-
2#3 Please check on http://stackoverflow.com/questions/456211/activity-restart-on-rotation-android – Yeo Jul 04 '11 at 18:04
-
#3 was not working for me, I have added this: `android:configChanges="orientation|keyboardHidden"` – Taha Jan 12 '12 at 11:55
-
17@EscobarCeaser, For Android 3.2 and above, you need `android:configChanges="orientation|screenSize"` – Pacerier Nov 20 '14 at 07:34
-
for me, solution #3 worked perfectly. The activity is not recreated after device rotation. Thanks! – Marcelo Noguti Dec 09 '14 at 13:54
-
10`android:configChanges="orientation|screenSize"` works for me. Thanks @Pacerier – Don Larynx May 21 '15 at 12:43
-
1please, update your answer case #3 with `android:configChanges="orientation|screenSize"` value for API >=13, because a lot of people see your answer as main! – radistao Jul 11 '15 at 18:02
-
1
-
1case #3 worked for me, But there is a problem with it when i m switching from portrait to landscape then screen is not fitting in device width. it is setting width size as per portrait's width. – shripal Jul 08 '16 at 14:31
-
-
More info about android:configChanges: https://developer.android.com/guide/topics/manifest/activity-element.html#config – mikep Jan 04 '18 at 11:29
-
-
-
-
It's working fine, But, Google does not recommend the most application in this approach. Please let me know the best way to achieve ??? – Manikandan K Oct 25 '18 at 08:02
Xion's answer was close, but #3 (android:configChanes="orientation"
) won't work unless the application has an API level of 12 or lower.
In API level 13 or above, the screen size changes when the orientation changes, so this still causes the activity to be destroyed and started when orientation changes.
Simply add the "screenSize" attribute like I did below:
<activity
android:name=".YourActivityName"
android:configChanges="orientation|screenSize">
</activity>
Now, when you change orientation (and screen size changes), the activity keeps its state and onConfigurationChanged()
is called. This will keep whatever is on the screen (ie: webpage in a Webview) when the orientation changes.
Learned this from this site: http://developer.android.com/guide/topics/manifest/activity-element.html
Also, this is apparently a bad practice so read the link below about Handling Runtime Changes:
http://developer.android.com/guide/topics/resources/runtime-changes.html

- 31,138
- 14
- 118
- 137

- 2,797
- 3
- 18
- 17
-
2
-
Works in Samsung Galaxy Nexus android 4.2.1 ! `android:configChanges="orientation|screenSize"` – Vincent Ducastel Mar 15 '14 at 13:40
-
Amazing. Have been working through code for so many hours to solve an issue i had with activity reloading and a video starting from the beginning. And here was the solution ! ++++1 – Mathias Asberg May 03 '14 at 17:28
-
7Bad practice? Pfft, real life people have deadlines and if it works, it works ;) – Kacy Feb 19 '15 at 00:45
-
-
-
@Kernald, my comment was old, after working on fragments, now I can confirm that this indeed is bad practise! – Sharp Edge Aug 07 '15 at 16:20
-
@SharpEdge The explanation can still be usefull to someone else. – Marc Plano-Lesay Aug 09 '15 at 18:02
-
-
@corbin : Google does not recommend the most application in this approach. Please let me know the best way to achieve ??? – Manikandan K Oct 25 '18 at 08:01
-
@ManikandanK per my answer Android recommends hooking into lifecycle methods as a best practice. Hopefully the link above can help you. Good luck! – corbin Nov 08 '18 at 22:17
-
You just have to go to the AndroidManifest.xml
and inside or in your activities labels, you have to type this line of code as someone up there said:
android:configChanges="orientation|screenSize"
So, you'll have something like this:
<activity android:name="ActivityMenu"
android:configChanges="orientation|screenSize">
</activity>
Hope it works!

- 18,895
- 5
- 32
- 63

- 393
- 3
- 10
<activity android:name="com.example.abc"
android:configChanges="orientation|screenSize"></activity>
Just add android:configChanges="orientation|screenSize"
in activity tab of manifest file.
So, Activity won't restart when orientation change.

- 27,862
- 20
- 113
- 121

- 562
- 6
- 17
-
Its working. But when I am using separate layout for each orientation layout.xml and layout-land.xml. its applying landscape layout for both orientation. If I use "orientation" only , Its giving what I expect but reloading. Could you help on this ? – Karthikeyan Ve Jun 24 '15 at 07:31
-
1@KarthikeyanVe That's the issue when you're trying to cheat. You'll *have* to restart your activity to change layout. Go ahead and just use the real solution, which is using `onSaveInstanceState()` and `onCreate()`. – Marc Plano-Lesay Aug 07 '15 at 12:38
-
-
@Weaboo the actual solution is linked in the question: https://developer.android.com/guide/topics/resources/runtime-changes.html – Marc Plano-Lesay Jan 15 '18 at 10:16
-
@MarcPlano-Lesay I've separate layout for portrait and landscape mode, if I open my app in landscape or portrait mode it works fine but whenever I try to change orientation on run time it closes my app, however when I login to my app and then navigate to MainActivity and try to change orientation it reloads that activity and works fine. Now the question is why? Is there a logical error in my code? Idk, but I've tried debugging the app several times, and I can't find anything. Moreover I don't want to restart my activity, why can't we just adapt respective layout whenever configuration changes? – Ahsan Jan 15 '18 at 13:49
It's my experience that it's actually better to just deal with the orientation changes properly instead of trying to shoehorn a non-default behavior.
You should save the image that's currently being displayed in onSaveInstanceState()
and restore it properly when your application runs through onCreate()
again.

- 27,862
- 20
- 113
- 121

- 30,048
- 8
- 87
- 96
-
#3 is a perfectly valid solution. It's the **exact** tool made to solve this exact problem. Why do you say that it's shoehorning? – Pacerier Nov 20 '14 at 07:36
-
1@Pacerier Just read the docs. "Note: Using `android:configChanges` should be avoided and used only as a last-resort. Please read *Handling Runtime Changes* for more information about how to properly handle a restart due to a configuration change." – Marc Plano-Lesay Aug 07 '15 at 12:42
-
This solution is by far the best working one. In your manifest file add
<activity
android:configChanges="keyboardHidden|orientation|screenSize"
android:name="your activity name"
android:label="@string/app_name"
android:screenOrientation="landscape">
</activity
And in your activity class add the following code
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//your code
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//your code
}
}

- 139
- 1
- 3
In manifiest file add to each activity this. This will help
android:configChanges = "orientation|keyboard|keyboardHidden|screenLayout|screenSize"

- 1,210
- 16
- 11
-
screenLayout worked for me, prevented refresh when resizing app for splitscreen on android – Terren Sep 25 '18 at 23:23
add android:configChanges="keyboardHidden|orientation|screenSize"
for all the app activities tags in manifest.

- 1,706
- 1
- 18
- 19
Just add this to your AndroidManifest.xml
<activity android:screenOrientation="landscape">
I mean, there is an activity tag, add this as another parameter. In case if you need portrait orientation, change landscape to portrait. Hope this helps.
-
im not looking to take away the ability to rotate the screen. i just dont want my app to reload... as i mentioned. – Jimmy D May 06 '11 at 15:11
-
ps... this doesnt work anyway. it just forces the app to display in a certain orientation. rotating the screen still reloads the activity – Jimmy D May 06 '11 at 15:41
just use : android:configChanges="keyboardHidden|orientation"

- 60,504
- 58
- 273
- 437

- 37
- 1
All above answers are not working for me. So, i have fixed by mentioning the label
with screenOrientation
like below. Now everything fine
<activity android:name=".activity.VideoWebViewActivity"
android:label="@string/app_name"
android:configChanges="orientation|screenSize"/>

- 1,516
- 2
- 28
- 49
http://animeshrivastava.blogspot.in/2017/08/activity-lifecycle-oncreate-beating_3.html
@Override
protected void onSaveInstanceState(Bundle b)
{
super.onSaveInstanceState(b);
String str="Screen Change="+String.valueOf(screenChange)+"....";
Toast.makeText(ctx,str+"You are changing orientation...",Toast.LENGTH_SHORT).show();
screenChange=true;
}

- 3,200
- 3
- 27
- 35

- 65
- 2
Prevent Activity to recreated Most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest.xml. Using this attribute your Activities won’t be recreated and all your views and data will still be there after orientation change.
<activity
android:name="com.example.test.activity.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"/>
this is work for me

- 1,025
- 2
- 8
- 16
Save the image details in your onPause()
or onStop()
and use it in the onCreate(Bundle savedInstanceState)
to restore the image.
EDIT:
More info on the actual process is detailed here http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle as it is different in Honeycomb than previous Android versions.

- 8,473
- 4
- 39
- 42
I dont know if the a best solution, but i describe it here:
First of all, you need certificate with you class Application of your app is in your manifest of this:
<application
android:name=".App"
...
Second, in my class App i did like this:
public class App extends Application {
public static boolean isOrientationChanged = false;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onConfigurationChanged(@NotNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ||
newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
isOrientationChanged = true;
}
}
}
Third, you need to set a flag to Orientation Change, in my case, I always set it when the previous activity within the app navigation is called, so only calling once when the later activity is created.
isOrientationChanged = false;
So every time I change the orientation of my screen in that context, I set it every time it changes this setting, it checks if there is a change in orientation, if so, it validates it based on the value of that flag.
Basically, I had to use it whenever I made an asynchronous retrofit request, which he called every moment that changed orientation, constantly crashing the application:
if (!isOrientationChanged) {
presenter.retrieveAddress(this, idClient, TYPE_ADDRESS);
}
I don't know if it's the most elegant and beautiful solution, but at least here it's functional :)
Add this code after the onCreate
,method in your activity containing the WebView
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
}

- 18,895
- 5
- 32
- 63

- 383
- 4
- 9
-
1How would this work? Aren't you just saying something explicitly to the runtime, what would otherwise be done by default? – Danielson Apr 09 '15 at 10:06