简体   繁体   English

Android锁定屏幕方向,保持onCreate()不变

[英]Android lock screen orientation and leaving onCreate() untouched

I am running something in onCreate() upon initialisation. 我在初始化时在onCreate()中运行了一些东西。

If a user rotates the screen, it recalls onCreate(). 如果用户旋转屏幕,它将调用onCreate()。

I want to disable screen rotation and let onCreate() run ONLY upon the initial initialisation. 我想禁用屏幕旋转,让onCreate()仅在初始化时运行。

Is it enough to add android:screenOrientation="portrait" to the manifest or will onCreate() still be run? 将android:screenOrientation =“ portrait”添加到清单中是否足够,还是会继续运行onCreate()?

Thanks! 谢谢!

If you put android:screenOrientation="portrait" in your Manifest the Phone doesnt handle orientation changes and onCreate() doesn't get called again. 如果您在清单中放入android:screenOrientation="portrait" ,则电话不会处理方向更改,并且不会再次调用onCreate() So: YES it is enough! 所以:是的,这就足够了!

You can easily check it if you set a Debug-Marker in your onCreate() and then rotate your phone! 如果您在onCreate()中设置了Debug-Marker,然后旋转手机,则可以轻松进行检查!

If you hold your Activity in Portrait or in Landscape , the rotation will no longer happens. 如果您将“ Activity保持为“ Portrait或“ Landscape ,则旋转将不再发生。 So the onCreate() will run to the end though you will try to rotate your device 因此,尽管您将尝试旋转设备,但onCreate()将运行到最后

Add in your manifest: 添加清单:

 android:configChanges="orientation"
 android:screenOrientation="portrait"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM