Android: set activity orientation BEFORE onCreate, but not in manifest (HDMI plugged in issue)

北城余情 提交于 2019-12-21 21:27:34

问题


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)


回答1:


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

        android:screenOrientation="behind"



回答2:


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,



来源:https://stackoverflow.com/questions/11192245/android-set-activity-orientation-before-oncreate-but-not-in-manifest-hdmi-plu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!