• Android横竖屏切换时的生命周期

    发布:51Code 时间: 2016-12-20 14:05

  • 1.关于这个问题,百度一大堆,但是鱼龙混杂,你如果自己认真看认真研究就会发现 ,没几个认真去验证过,特别是有人说横竖屏切换,横屏切换竖屏会导致两次生命周期,那么我们就...

  • 1.关于这个问题,百度一大堆,但是鱼龙混杂,你如果自己认真看认真研究就会发现 ,没几个认真去验证过,特别是有人说横竖屏切换,横屏切换竖屏会导致两次生命周期,那么我们就来看一下这个结论是否正确。

    2.我以当前主流最低兼容版本4.4为例做研究,通过为actvity各个生命周期添加Log来研究这个问题,中国现在目前主流的还是4.x,5.0,6.0的还是少数旗舰机才有,此处不做研究,所有研究结论均基于4.4为基准。

    3.部分示例代码如下:

        @Override

        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

            Log.d("MainActivity", "onCreate");

        }

     

        @Override

        protected void onStart() { 

        super.onStart();

        Log.d("MainActivity", "onStart");

        }

        

        @Override

        protected void onResume() { 

        super.onResume();

        Log.d("MainActivity", "onResume");

        }

        

        @Override

        protected void onSaveInstanceState(Bundle outState) { 

        super.onSaveInstanceState(outState);

        Log.d("MainActivity", "onSaveInstanceState");

        }

        

        @Override

        protected void onPause() { 

        super.onPause();

        Log.d("MainActivity", "onPause");

        }

        

        @Override

        protected void onStop() { 

        super.onStop();

        Log.d("MainActivity", "onStop");

        }

        

        @Override

        protected void onDestroy() { 

        super.onDestroy();

        Log.d("MainActivity", "onDestroy");

        }

        

        @Override

        protected void onRestoreInstanceState(Bundle savedInstanceState) { 

        super.onRestoreInstanceState(savedInstanceState);

        Log.d("MainActivity", "onRestoreInstanceState");

    }

     

    @Override

        public void onConfigurationChanged(Configuration newConfig) { 

        super.onConfigurationChanged(newConfig);

        Log.d("MainActivity", "onConfigurationChanged");

    }

        

    4.在manifest.xml文件中这个activity没有设置orientation属性,也就是支持横竖屏切换:

    a.竖屏进入:

    11-01 13:04:16.086: D/MainActivity(2397): onCreate

    11-01 13:04:16.086: D/MainActivity(2397): onStart

    11-01 13:04:16.086: D/MainActivity(2397): onResume

    b.竖屏切到横屏:

    11-01 13:04:53.018: D/MainActivity(2397): onPause

    11-01 13:04:53.018: D/MainActivity(2397): onSaveInstanceState

    11-01 13:04:53.018: D/MainActivity(2397): onStop

    11-01 13:04:53.018: D/MainActivity(2397): onDestroy

    11-01 13:04:53.046: D/MainActivity(2397): onCreate

    11-01 13:04:53.046: D/MainActivity(2397): onStart

    11-01 13:04:53.046: D/MainActivity(2397): onRestoreInstanceState

    11-01 13:04:53.046: D/MainActivity(2397): onResume

    c.横屏切竖屏:

    11-01 13:05:46.338: D/MainActivity(2397): onPause

    11-01 13:05:46.338: D/MainActivity(2397): onSaveInstanceState

    11-01 13:05:46.338: D/MainActivity(2397): onStop

    11-01 13:05:46.338: D/MainActivity(2397): onDestroy

    11-01 13:05:46.362: D/MainActivity(2397): onCreate

    11-01 13:05:46.386: D/MainActivity(2397): onStart

    11-01 13:05:46.386: D/MainActivity(2397): onRestoreInstanceState

    11-01 13:05:46.386: D/MainActivity(2397): onResume

     

    备注:我的结论是,横竖屏切换的生命周期没什么不同,另外OnPasuse先执行,然后onSaveInstanceState()

     

    d.有人说在activity配置里面加上configeChanges="orientaion"会有不同,那我们也加上这个,然后执行a操作,来验证这个结论:

    11-01 13:09:10.278: D/MainActivity(5828): onCreate

    11-01 13:09:10.278: D/MainActivity(5828): onStart

    11-01 13:09:10.278: D/MainActivity(5828): onResume

     

    e.执行步骤b,切换到横屏状态:

    11-01 13:09:27.238: D/MainActivity(5828): onPause

    11-01 13:09:27.238: D/MainActivity(5828): onSaveInstanceState

    11-01 13:09:27.238: D/MainActivity(5828): onStop

    11-01 13:09:27.238: D/MainActivity(5828): onDestroy

    11-01 13:09:27.242: D/MainActivity(5828): onCreate

    11-01 13:09:27.242: D/MainActivity(5828): onStart

    11-01 13:09:27.246: D/MainActivity(5828): onRestoreInstanceState

    11-01 13:09:27.246: D/MainActivity(5828): onResume

     

    f.横屏切换到竖屏状态:

    11-01 13:09:57.642: D/MainActivity(5828): onPause

    11-01 13:09:57.642: D/MainActivity(5828): onSaveInstanceState

    11-01 13:09:57.642: D/MainActivity(5828): onStop

    11-01 13:09:57.642: D/MainActivity(5828): onDestroy

    11-01 13:09:57.646: D/MainActivity(5828): onCreate

    11-01 13:09:57.646: D/MainActivity(5828): onStart

    11-01 13:09:57.646: D/MainActivity(5828): onRestoreInstanceState

    11-01 13:09:57.646: D/MainActivity(5828): onResume

    结论:然并卵!一样一样的!

     

    g.我们把configechanges修改为:configchanges="orientation|keyboardhidden",重新进入activity:

    11-01 13:11:31.270: D/MainActivity(7471): onCreate

    11-01 13:11:31.270: D/MainActivity(7471): onStart

    11-01 13:11:31.270: D/MainActivity(7471): onResume

     

    h.竖屏切换到横屏:

    11-01 13:11:53.082: D/MainActivity(7471): onPause

    11-01 13:11:53.082: D/MainActivity(7471): onSaveInstanceState

    11-01 13:11:53.082: D/MainActivity(7471): onStop

    11-01 13:11:53.082: D/MainActivity(7471): onDestroy

    11-01 13:11:53.118: D/MainActivity(7471): onCreate

    11-01 13:11:53.138: D/MainActivity(7471): onStart

    11-01 13:11:53.138: D/MainActivity(7471): onRestoreInstanceState

    11-01 13:11:53.138: D/MainActivity(7471): onResume

    i.横屏切换到竖屏:

     

    11-01 13:12:08.638: D/MainActivity(7471): onPause

    11-01 13:12:08.638: D/MainActivity(7471): onSaveInstanceState

    11-01 13:12:08.638: D/MainActivity(7471): onStop

    11-01 13:12:08.638: D/MainActivity(7471): onDestroy

    11-01 13:12:08.642: D/MainActivity(7471): onCreate

    11-01 13:12:08.642: D/MainActivity(7471): onStart

    11-01 13:12:08.642: D/MainActivity(7471): onRestoreInstanceState

    11-01 13:12:08.642: D/MainActivity(7471): onResume

     

    j.似乎没什么卵用,再修改一下configchanges为orientation|keyboardHidden|screenSize,重新进入:

     

    11-01 13:13:02.310: D/MainActivity(8537): onCreate

    11-01 13:13:02.310: D/MainActivity(8537): onStart

    11-01 13:13:02.310: D/MainActivity(8537): onResume

    k.切换到横屏: 

    11-01 13:15:22.394: D/MainActivity(10092): onConfigurationChanged

    L.横屏切竖屏:

    11-01 13:15:49.334: D/MainActivity(10092): onConfigurationChanged

    总结:在4.4的平台上,横竖屏切换生命周期各执行一次,不存在多执行一次的情况,加入configurationChanges="orientaion|keyboardhidden"对生命周期不影响,再加入screensize的时候,不执行重建工作,只调用onConfigurationChanged()方法。本次研究只基于4.4的平台,若有什么不对的情况,请大家纠正!

  • 上一篇:Android scroller的原理分析

    下一篇:Android技术题之DOM解析器

网站导航
Copyright(C)51Code软件开发网 2003-2021 , 沪ICP备05003035号-6