美文网首页
Activity横竖屏切换

Activity横竖屏切换

作者: 默默奋斗的木头人 | 来源:发表于2017-05-04 22:44 被阅读0次

    测试手机:小米5
    第一种情况:
    普通Activity,1【无其他参数 】,2【configchange="orientation"】
    (1)第一次启动时:onCreate()-->onStart()-->onResume();
    (2)然后 由竖屏切位横屏 : onPause() --> onSaveInstanceState(1) --> onStop() --> onDestory() --> onCreate() --> onStart() --> onRestoreInstanceState(1) -- > onResume();
    (3) 后面每次翻转都是上面的第二步的流程

    第二种情况
    普通Activity,【configchanges="orientation|screensize",这个属性的意思是同时满足屏幕旋转和尺寸改变时,会调用onConfiguratonChange(),否者会重新启动Activity】,在官方文档中说的是, If a configuration change involves any that you do not handle, however, the activity will still be restarted andonConfigurationChanged(Configuration)
    will not be called.大概意思是说,当configchanges属性值中,不包含配置改变的某个原因,还是会重新调用Activity的逻辑,就比如只写了一个orientation时,屏幕从竖屏变为横屏,这是不仅方向改了,屏幕大小也改变了,所以只设置了其中一个的话,是没起到实际作用的

    可以通过如下方式禁用掉横竖屏切换:
    在AndroidManifest.xml中
    竖屏:android:screenOrientation="portrait"
    横屏:android:screenOrientation="landscape"

    java代码中
    竖屏:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATIN_PORTRAIT)
    横屏:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

    彻底禁止翻转,忽略重力感应带来的切换
    android:screenOrientation="nosensor"

    相关文章

      网友评论

          本文标题:Activity横竖屏切换

          本文链接:https://www.haomeiwen.com/subject/imadtxtx.html