Android: set activity orientation BEFORE onCreate,

2019-04-12 21:00发布

I can perfectly manage orientation change programatically, by using setRequestedOrientation in onCreate.

Everything works really fine, until I plug a HDMI cable. After this, the tablet "wants" to be in landscape mode. So when I open an activity, it shows first in "landscape" and then right after it shows in "portrait", (because I'm calling setRequestedOrientation(variable_with_orientation_desired_by_the_user) where variable=ActivityInfo.SCREEN_ORIENTATION_PORTRAIT in onCreate As you can "see", my app keeps rotating on every activity the user opens;

If I set the orientantion for activity in the manifest, this problem is partially gone, since the orientation in manifest is the same that the user has picked up. If they are different, the same behavior starts again.

I tried this, but with no success:

@Override
public void onCreate(final Bundle saved)
{
    setRequestedOrientation(ScreenOrientation);
    super.onCreate(icicle);
}

So, is there any way to tell to android what orientantion it must create my activity, before it does create it? (But it cannot be in manifest)

2条回答
SAY GOODBYE
2楼-- · 2019-04-12 21:29

I solved it using this declaration in manifest:

android:screenOrientation="locked"

on every activity declared in manifest with this issue.

And keep using setRequestedOrientation() programatically to define if landscape or portrait orientation within onCreate() method,

Hope it helps someone,

查看更多
闹够了就滚
3楼-- · 2019-04-12 21:49

I solved it by setting screenOrientation to "behind" in manifest file:

        android:screenOrientation="behind"
查看更多
登录 后发表回答