Search code examples
androidandroid-activityandroid-orientationandroid-configchanges

Set orientation programmatically, without activity-recreation?


Whenever I use setRequestedOrientation(int) it behaves differently from when I declare the orientation in my Manifest.xml. (this is expected)

I read that (from docs):

If the activity is currently in the foreground or otherwise impacting the screen orientation, the screen will immediately be changed (possibly causing the activity to be restarted)

Practically this means that whenever an app is started, while the device has an orientation that is NOT the orientation that will be set with setRequestedOrientation(int), the activity will be created twice (once 'normally' and once to reflect requested orientation).

As of now it is not worthwhile to make my activites 100% config-change-proof, what I would like is a way to set the requested orientation, in code, without having to create my activities twice. Is there a way?

(Preferably without using hackish workaround such as setting the 'orientation' flag in configChanges)

EDIT:

Accepted answer is right, it simply isn't logical (doesn't make sense). For new readers, what I did was add a stub-activity (mainly empty activity that doesn't do much). All it does is use getRequestedOrientation() when it differs from what I want it to be, I will call setRequestedOrientation(int), otherwise start the activity I want and in its onCreate also call the setRequestedOrientation(int) to keep it in requested orientation.


Solution

  • No, that doesn't make sense. If you set the orientation programmatically, the Activity is already running before you can do anything to change the duplicate creation for each orientation.