美文网首页Android基础知识梳理
Activity和Fragment的生命周期

Activity和Fragment的生命周期

作者: hahauha | 来源:发表于2020-06-30 15:07 被阅读0次

    1、activity嵌套frament ,生命周期方法执行顺序

    activity和fragment生命周期方法.png

    2、横竖屏切换activity生命周期方法调用情况(android10)

    (1)不设置Activity的android:configChanges
    打开activity

    >>>>>>>ui.HomeActivity: onCreate()
    >>>>>>>ui.HomeActivity: onStart()
    >>>>>>>ui.HomeActivity: onResume()
    

    切横屏

    >>>>>>>ui.HomeActivity: onPause()
    >>>>>>>ui.HomeActivity: onStop()
    >>>>>>>ui.HomeActivity: onDestroy()
    >>>>>>>ui.HomeActivity: onCreate()
    >>>>>>>ui.HomeActivity: onStart()
    >>>>>>>ui.HomeActivity: onResume()
    

    切回竖屏

    >>>>>>>ui.HomeActivity: onPause()
    >>>>>>>ui.HomeActivity: onStop()
    >>>>>>>ui.HomeActivity: onDestroy()
    >>>>>>>ui.HomeActivity: onCreate()
    >>>>>>>ui.HomeActivity: onStart()
    >>>>>>>ui.HomeActivity: onResume()
    

    (2)设置Activity的 android:configChanges="orientation"
    切换横竖屏只执行了

    >>>>>>>ui.HomeActivity: onConfigurationChanged()
    

    (3)设置Activity的android:configChanges=”orientation |keyboardHidden|screenSize”
    切换横竖屏只执行了

    >>>>>>>ui.HomeActivity: onConfigurationChanged()
    

    注意

    Android 3.2(API 13),设置android:configChanges="orientation|keyboardHidden"起作用,
    Android 3.2(API 13)之后,还必须设置"screenSize"
    android:configChanges="keyboardHidden|orientation|screenSize",否则不会调用onConfigurationChanged()方法

    3、按home,黑屏时,activity和fragment生命周期方法执行情况

    >>>>>>>OneFragment: onPause()
    >>>>>>>ui.HomeActivity: onPause()
    >>>>>>>OneFragment: onStop()
    >>>>>>>ui.HomeActivity: onStop()
    

    恢复

    >>>>>>>OneFragment: onStart()
    >>>>>>>ui.HomeActivity: onStart()
    >>>>>>>ui.HomeActivity: onResume()
    >>>>>>>OneFragment: onResume()
    

    相关文章

      网友评论

        本文标题:Activity和Fragment的生命周期

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