Android activity/fragment的生命周期记录

一.activity

1.onCreate()    创建活动,构造ui的地方,只调用一次

2.onStart()        使得活动开始显示,可以调用多次

3.onResume()   显示ui

4.onPause ()     页面被隐藏或者后台运行,但没有完成退出时,调用

5.onStop()         活动停止

6.onRestart()      重新回到前台

7.onDestroy()     销毁

1.建立demo,生成A,B,C,D界面。每个页面的生命周期事件都log出来提示。

2.测试standard模式去启动这个四个界面:

         A->B->C->D:

   

       A->B->C->D->A:

打印出来的各自的生命周期事件:

2020-07-03 12:02:03.060 11601-11601/com.example.test_01 I/A界面: onCreate
2020-07-03 12:02:03.061 11601-11601/com.example.test_01 I/A界面: onStart
2020-07-03 12:02:03.063 11601-11601/com.example.test_01 I/A界面: onResume
2020-07-03 12:02:08.228 11601-11601/com.example.test_01 I/A界面: onPause
2020-07-03 12:02:08.247 11601-11601/com.example.test_01 I/B界面: onCreate
2020-07-03 12:02:08.248 11601-11601/com.example.test_01 I/B界面: onStart
2020-07-03 12:02:08.250 11601-11601/com.example.test_01 I/B界面: onResume
2020-07-03 12:02:08.664 11601-11601/com.example.test_01 I/A界面: onStop
2020-07-03 12:02:09.819 11601-11601/com.example.test_01 I/B界面: onPause
2020-07-03 12:02:09.840 11601-11601/com.example.test_01 I/C界面: onCreate
2020-07-03 12:02:09.842 11601-11601/com.example.test_01 I/C界面: onStart
2020-07-03 12:02:09.843 11601-11601/com.example.test_01 I/C界面: onResume
2020-07-03 12:02:10.251 11601-11601/com.example.test_01 I/B界面: onStop
2020-07-03 12:02:11.216 11601-11601/com.example.test_01 I/C界面: onPause
2020-07-03 12:02:11.234 11601-11601/com.example.test_01 I/D界面: onCreate
2020-07-03 12:02:11.235 11601-11601/com.example.test_01 I/D界面: onStart
2020-07-03 12:02:11.237 11601-11601/com.example.test_01 I/D界面: onResume
2020-07-03 12:02:11.656 11601-11601/com.example.test_01 I/C界面: onStop
2020-07-03 12:07:36.594 11601-11601/com.example.test_01 I/D界面: onPause
2020-07-03 12:07:36.612 11601-11601/com.example.test_01 I/A界面: onCreate
2020-07-03 12:07:36.613 11601-11601/com.example.test_01 I/A界面: onStart
2020-07-03 12:07:36.615 11601-11601/com.example.test_01 I/A界面: onResume
2020-07-03 12:07:37.031 11601-11601/com.example.test_01 I/D界面: onStop

 

可以看出默认启动模式是,每一次去跳转一个activity都是去oncreate一个界面。

当我从A->B->C->D->A,点击返回按钮(A->D->C->B->A),事件是:

2020-07-03 12:13:57.727 11601-11601/com.example.test_01 I/A界面: onPause
2020-07-03 12:13:57.743 11601-11601/com.example.test_01 I/D界面: onRestart
2020-07-03 12:13:57.744 11601-11601/com.example.test_01 I/D界面: onStart
2020-07-03 12:13:57.745 11601-11601/com.example.test_01 I/D界面: onResume
2020-07-03 12:13:58.170 11601-11601/com.example.test_01 I/A界面: onStop
2020-07-03 12:13:58.170 11601-11601/com.example.test_01 I/A界面: onDestroy
2020-07-03 12:14:19.576 11601-11601/com.example.test_01 I/D界面: onPause
2020-07-03 12:14:19.588 11601-11601/com.example.test_01 I/C界面: onRestart
2020-07-03 12:14:19.590 11601-11601/com.example.test_01 I/C界面: onStart
2020-07-03 12:14:19.590 11601-11601/com.example.test_01 I/C界面: onResume
2020-07-03 12:14:20.001 11601-11601/com.example.test_01 I/D界面: onStop
2020-07-03 12:14:20.001 11601-11601/com.example.test_01 I/D界面: onDestroy
2020-07-03 12:14:23.023 11601-11601/com.example.test_01 I/C界面: onPause
2020-07-03 12:14:23.033 11601-11601/com.example.test_01 I/B界面: onRestart
2020-07-03 12:14:23.035 11601-11601/com.example.test_01 I/B界面: onStart
2020-07-03 12:14:23.036 11601-11601/com.example.test_01 I/B界面: onResume
2020-07-03 12:14:23.454 11601-11601/com.example.test_01 I/C界面: onStop
2020-07-03 12:14:23.454 11601-11601/com.example.test_01 I/C界面: onDestroy
2020-07-03 12:14:36.483 11601-11601/com.example.test_01 I/B界面: onPause
2020-07-03 12:14:36.494 11601-11601/com.example.test_01 I/A界面: onRestart
2020-07-03 12:14:36.496 11601-11601/com.example.test_01 I/A界面: onStart
2020-07-03 12:14:36.496 11601-11601/com.example.test_01 I/A界面: onResume
2020-07-03 12:14:36.907 11601-11601/com.example.test_01 I/B界面: onStop
2020-07-03 12:14:36.908 11601-11601/com.example.test_01 I/B界面: onDestroy
2020-07-03 12:14:41.847 11601-11601/com.example.test_01 I/A界面: onPause
2020-07-03 12:14:42.246 11601-11601/com.example.test_01 I/A界面: onStop
2020-07-03 12:14:42.246 11601-11601/com.example.test_01 I/A界面: onDestroy

 

二.fragment

1.onAttach()    与活动关联调用

2.onCreate()    初次创建片段

3.onCreateView() 为创建片段,返回根布局视图

4.onActivityCreated() 告诉片段,活动onCreate已经创建

5.onStart()       告诉片段可以开始显示ui

6.onResume()   显示ui

7.onPause ()     页面被隐藏或者后台运行,但没有完成退出时,调用

8.onStop()         活动停止

9.onDestroyView() 允许片段可以释放资源

10.onDestroy()   允许片段清理资源

11.onDetach()    片段与活动解除关联

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值