Android screen orientation change have different behaviour on tablet

泄露秘密 提交于 2019-12-01 13:18:07

问题


I have activity with:

android:screenOrientation="portrait"

and code:

if(blah blah blah)
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

in activity onCreate. 1. When test on smartphone "setRequestedOrientation" causes destroy activity and create again with desired screen orientation. 2. When test on tablet device this code runs only once activity. After "setRequestedOrientation" it just continues without destroy and create again activity. This happens when start code without debuger or without breakpoint in onCreate. If I stop in onCreate function - I see in logs - destroying and onCreate again. How to catch specific situation with tablet?

UPDATE: - Problem with tablet occurs when 'Orientation lock' if off. If 'Orientation lock' is enabled program works in same way as in smartphone.

UPDATE2: - Is it possible to set preffered screen orientation to whole application. So when activity starts to know desired orientation. This will make unnessesary call 'setScreenOrientation' and avoid second activity creation.


回答1:


No.. you cannot set preffered screen orientation for whole application... instead you should call for setScreenOrientation in respective activies only... Refere this link here...




回答2:


After whole day spent with this issue I made following changes in my application: 1. Every activity /without first/ are declared as android:screenOrientation="behind". 2. In first activity I set preferred screen orientation using user selected mode /from my menu. 3. I call setRequestedOrientation only when user changed mode in my menu options.

This causes activities to be created only once. Finaly :)




回答3:


You can set

<activity ActivityName = "test_activity"
android:configChanges="orientation">
</activity>

in your android manifest file. It will not allow activity to restart on orientation change but onconfigurationchange will be called normaly. Later you can getdisplayorientation in onconfigurationchange and set whatever you want.



来源:https://stackoverflow.com/questions/10297326/android-screen-orientation-change-have-different-behaviour-on-tablet

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