美文网首页android开发
旋转后的2个activtiy生命周期变动

旋转后的2个activtiy生命周期变动

作者: 向前的zz | 来源:发表于2020-03-18 10:18 被阅读0次

    AActivity启动BActivity旋转的生命周期变化

    这个的AActivity由MainActivity代替

    AActivity -> BActivity 的生命周期过程

    MainActivity: onPause
    BActivity: ----> onCreate
    BActivity: ----> onStart
    BActivity: ----> onResume
    MainActivity: onStop
    MainActivity: onSaveInstanceState
    

    BActivity启动后的情况下,反转手机屏幕

    BActivity: onPause
    BActivity: onStop
    BActivity: onSaveInstanceState
    BActivity: onDestroy
    BActivity: onCreate
    BActivity: onStart
    BActivity: onRestoreInstanceState
    BActivity: onResume
    

    此时如果息屏了,然后手机又会变成竖屏

    BActivity: onPause
    BActivity: onStop
    BActivity: onSaveInstanceState
    BActivity: onDestroy
    BActivity: onCreate
    BActivity: onStart
    BActivity: onRestoreInstanceState
    BActivity: onResume
    BActivity: onPause
    BActivity: onStop
    BActivity: onSaveInstanceState
    

    这个时候注意:如果BActivity现在还是横屏的状态,按了返回键之后 , MainActivity的生命周期变化情况如下:

    BActivity: ----> onPause
    MainActivity: onDestroy
    MainActivity: onCreate
    MainActivity: onStart
    MainActivity: onRestoreInstanceState
    MainActivity: onResume
    BActivity: ----> onStop
    BActivity: ----> onDestroy
    

    但是如果当旋转BActivity(横屏)之后,然后再把BActivity旋转回来(竖屏),在按返回键退出BActivity,然后MainActivity此时会是一个正常的生命周期

    BActivity: ----> onPause
    MainActivity: onRestart
    MainActivity: onStart
    MainActivity: onResume
    BActivity: ----> onStop
    BActivity: ----> onDestroy
    

    配置android:configChanges

    //配置了这个之后,就旋转屏幕就不会走生命周期了
    //不重新重建Activity
    //https://www.jianshu.com/p/4d3441757ab4
    android:configChanges="keyboardHidden|orientation|screenSize"
    

    //屏幕息屏到开屏的时候

    MainActivity: onRestart
    MainActivity: onStart
    MainActivity: onResume
    

    相关文章

      网友评论

        本文标题:旋转后的2个activtiy生命周期变动

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