Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

android - Screen orientation and values in manifest.xml

I want to use all activities in my form in landscape or portrait. When user select orientation - this is valid for all activities. Tried with "behind" option orientation. According to Google - orientation will depend on previous activity.

My first activity use setRequestedOrientation to set selected from user orientation.

Next activities have to follow same orientation. Do I have to put setRequestedOrientation in their code too? Or really on 'behind' parameter in manifest? Putting setRequestedOrientation may be cause onCreate again?

UPDATE:
Tried "portrait" and setRequestedOrientation() - result is onCreate was called 2 times.

Problem is in next activity -> because of "portrait" in first activity - android started next activity with same orientation. It ignores "landscape" orientation which was set by me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to have fixed orientation for your activities then you can use-

android:screenOrientation="portrait"

android:screenOrientation="sensorPortrait"

as an attribute to that activity in that manifest. But if you want to set the orientation runtime depending upon what was the previous orientation while launching the application, you need to check for the previous orientation in onCreate() and then set it to that value programmatically there itself using setRequestedOrientation()

UPDATE: As pointed by @s.co.tt use android:screenOrientation="sensorPortrait" for a better support on tablets.

For more details on the different values for android:screenOrientation and what each of them do, look at the docs:

https://developer.android.com/guide/topics/manifest/activity-element.html#screen


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...