Android中Fragment与Activity的生命周期对比
作者:高杰才_Android 时间:2021-12-12 06:28:01
Fragment必须是依存于Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期。官网这张图很好的说明了俩者的关系:
可以看到Fragment比Activity多了几个额外的生命周期回调函数:
onAttach(Activity);//当Activity与Fragment发生关联时调用
onCreateView(LayoutInflater,ViewGroup,Bundle);//创建该Fragment的视图
onActivityCreate(bundle);//当Activity的onCreate();方法返回时调用
onDestoryView();//与onCreateView相对应,当改Fragment被移除时调用
onDetach();//与onAttach()相对应,当Fragment与Activity的关联被取消时调用
注意:除了onCreateView,其他的所有方法如果你重写了,必须调用父类对于该方法的实现。
场景演示 :
切换到该Fragment
11-29 14:26:35.095: D/AppListFragment(7649): onAttach
11-29 14:26:35.095: D/AppListFragment(7649): onCreate
11-29 14:26:35.095: D/AppListFragment(7649): onCreateView
11-29 14:26:35.100: D/AppListFragment(7649): onActivityCreated
11-29 14:26:35.120: D/AppListFragment(7649): onStart
11-29 14:26:35.120: D/AppListFragment(7649): onResume
屏幕灭掉:
11-29 14:27:35.185: D/AppListFragment(7649): onPause
11-29 14:27:35.205: D/AppListFragment(7649): onSaveInstanceState
11-29 14:27:35.205: D/AppListFragment(7649): onStop
屏幕解锁
11-29 14:33:13.240: D/AppListFragment(7649): onStart
11-29 14:33:13.275: D/AppListFragment(7649): onResume
切换到其他Fragment:
11-29 14:33:33.655: D/AppListFragment(7649): onPause
11-29 14:33:33.655: D/AppListFragment(7649): onStop
11-29 14:33:33.660: D/AppListFragment(7649): onDestroyView
切换回本身的Fragment:
11-29 14:33:55.820: D/AppListFragment(7649): onCreateView
11-29 14:33:55.825: D/AppListFragment(7649): onActivityCreated
11-29 14:33:55.825: D/AppListFragment(7649): onStart
11-29 14:33:55.825: D/AppListFragment(7649): onResume
回到桌面
11-29 14:34:26.590: D/AppListFragment(7649): onPause
11-29 14:34:26.880: D/AppListFragment(7649): onSaveInstanceState
11-29 14:34:26.880: D/AppListFragment(7649): onStop
回到应用
11-29 14:36:51.940: D/AppListFragment(7649): onStart
11-29 14:36:51.940: D/AppListFragment(7649): onResume
退出应用
11-29 14:37:03.020: D/AppListFragment(7649): onPause
11-29 14:37:03.155: D/AppListFragment(7649): onStop
11-29 14:37:03.155: D/AppListFragment(7649): onDestroyView
11-29 14:37:03.165: D/AppListFragment(7649): onDestroy
11-29 14:37:03.165: D/AppListFragment(7649): onDetach
比Activity多了一些生命周期,完整和Activity对接上,大家好好利用。
标签:Fragment,Activity
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java自带定时任务ScheduledThreadPoolExecutor实现定时器和延时加载功能
2023-02-24 13:52:03
关于Mybatis插入对象时空值的处理
2023-08-28 21:58:00
javaweb 项目初始配置的方法步骤
2021-12-23 17:34:23
![](https://img.aspxhome.com/file/2023/8/63418_0s.jpg)
android学习笔记之View的滑动
2022-12-30 07:51:36
![](https://img.aspxhome.com/file/2023/4/130304_0s.gif)
Spring Boot 在启动时进行配置文件加解密的方法详解
2023-11-12 17:13:54
spring cloud Feign使用@RequestLine遇到的坑
2023-12-15 09:18:16
![](https://img.aspxhome.com/file/2023/5/78035_0s.png)
MyBatis关闭一级缓存的两种方式(分注解和xml两种方式)
2023-09-02 10:18:59
关于Android中ListView嵌套GridView的问题
2023-09-16 10:56:39
Android单片机与蓝牙模块通信实例代码
2023-02-21 17:21:05
![](https://img.aspxhome.com/file/2023/5/128965_0s.jpg)
Maven工程打包jar的多种方式
2022-12-15 06:54:46
![](https://img.aspxhome.com/file/2023/6/67686_0s.png)
Java并发框架:Executor API详解
2021-10-31 14:30:28
基于使用BeginInvoke,EndInvoke异步调用委托的实现代码
2023-04-29 09:46:49
IDEA2020.1常用配置说明
2023-01-09 02:11:50
![](https://img.aspxhome.com/file/2023/9/69679_0s.png)
Java如何把数组转换为ArrayList
2021-08-14 19:59:45
C# WPF 通过委托实现多窗口间的传值的方法
2022-03-27 19:02:23
![](https://img.aspxhome.com/file/2023/4/126124_0s.png)
Spring Boot应用发布到Docker的实现
2021-08-21 09:53:50
java POI解析Excel 之数据转换公用方法(推荐)
2023-06-10 11:04:53
C# 动画窗体(AnimateWindow)的小例子
2022-10-08 09:31:07
浅谈Java面向接口编程
2021-07-25 16:29:58
C#使用windows服务开启应用程序的方法
2022-08-14 04:37:14