Change screen orientation in Android without rebooting

I want to programmatically change the orientation when working with an Android application with these lines of code:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); 

They still work, but the main problem is that all the activity restarts when the screen orientation changes, and I do not want this. Is it possible? Thank.

EDIT: Well, after I found out what I was missing. I had to include also "screenSize" in the configChanges property, so

 android:configChanges="orientation|screenSize" 

decided all this.

+28
android screen-orientation
Feb 14 '13 at 11:26
source share
5 answers

See edit @luisfer. To target Android 3.2 and above, you need BOTH

 android:configChanges="orientation|screenSize" 

http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

+27
Jul 22 '13 at 23:30
source share

You need to override onSaveInstanceState (Bundle savedInstanceState) and write the application state values โ€‹โ€‹you want to change to the Bundle parameter

+4
Feb 14 '13 at 11:28
source share

In the AndroidManifest file, add android:configChanges="orientation" for the action you want to handle with that orientation

The work uses the onConfigurationChange overrided method. The task you want to handle orientation changes with.

+1
Feb 14 '13 at 11:34
source share

Taken here:

Android, how not to destroy the action when turning the device?

Add

android:configChanges="orientation"

To your android manifest.

cm

http://developer.android.com/guide/topics/manifest/activity-element.html#config

+1
Feb 14 '13 at 11:37
source share

Call this method and install the manifest file

 android:configChanges="orientation|screenSize|keyboardHidden" public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Checks the orientation of the screen if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); } } 
+1
Sep 01 '15 at 8:53
source share



All Articles