大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
在学Fragment之前肯定学过了Activity,Activity有属于自己的生命周期,Fragment基本上和activity 大体一样,但是有自己特有的生命周期方法,下面我们一起来看一下。
说白了主要看两张图,和运行代码:
一. Fragment的生命周期
二. 与Activity生命周期的对比
三. 场景演示 :
①启动Fragment
07-05 11:14:48.791 16273-16273/com.mypractice E/—-A Activity—-: onCreate
07-05 11:14:48.809 16273-16273/com.mypractice E/—-A Activity—-: onStart
07-05 11:14:48.809 16273-16273/com.mypractice E/—-A Fragment—-: onAttach
07-05 11:14:48.809 16273-16273/com.mypractice E/—-A Fragment—-: onCreate
07-05 11:14:48.809 16273-16273/com.mypractice E/—-A Fragment—-: onCreateView
07-05 11:14:48.813 16273-16273/com.mypractice E/—-A Fragment—-: onActivityCreated
07-05 11:14:48.813 16273-16273/com.mypractice E/—-A Fragment—-: onStart
07-05 11:14:48.813 16273-16273/com.mypractice E/—-A Activity—-: onResume
07-05 11:14:48.813 16273-16273/com.mypractice E/—-A Fragment—-: onResume
②锁屏(注意此处Fragment的onSaveInstanceState方法执行,但是Activity的此方法并未执行,解释见文末尾注释):
07-05 11:18:36.007 16273-16273/com.mypractice E/—-A Activity—-: onPause
07-05 11:18:36.007 16273-16273/com.mypractice E/—-A Fragment—-: onPause
07-05 11:18:36.007 16273-16273/com.mypractice E/—-A Fragment—-:onSaveInstanceState
07-05 11:18:36.193 16273-16273/com.mypractice E/—-A Activity—-: onStop
07-05 11:18:36.194 16273-16273/com.mypractice E/—-A Fragment—-: onStop
③解锁:
07-05 11:19:20.898 16273-16273/com.mypractice E/—-A Activity—-: onRestart
07-05 11:19:20.899 16273-16273/com.mypractice E/—-A Activity—-: onStart
07-05 11:19:20.899 16273-16273/com.mypractice E/—-A Fragment—-: onStart
07-05 11:19:20.910 16273-16273/com.mypractice E/—-A Activity—-: onResume
07-05 11:19:20.910 16273-16273/com.mypractice E/—-A Fragment—-: onResume
④切换到其它 Fragment(A –> B):
07-05 11:21:02.961 16273-16273/com.mypractice E/—-A Activity—-: onPause
07-05 11:21:02.961 16273-16273/com.mypractice E/—-A Fragment—-: onPause
07-05 11:21:02.999 16273-16273/com.mypractice E/—-B Fragment—-: onAttach
07-05 11:21:02.999 16273-16273/com.mypractice E/—-B Fragment—-: onCreate
07-05 11:21:03.000 16273-16273/com.mypractice E/—-B Fragment—-: onCreateView
07-05 11:21:03.002 16273-16273/com.mypractice E/—-B Fragment—-: onActivityCreated
07-05 11:21:03.002 16273-16273/com.mypractice E/—-B Fragment—-: onStart
07-05 11:21:03.003 16273-16273/com.mypractice E/—-B Fragment—-: onResume
07-05 11:21:03.900 6921-6921/kj.com.mypractice E/—-A Fragment—-: onSaveInstanceState
07-05 11:21:03.452 16273-16273/com.mypractice E/—-A Activity—-: onStop
07-05 11:21:03.452 16273-16273/com.mypractice E/—-A Fragment—-: onStop
⑤Back键返回(B –> A):
07-05 11:23:32.094 16273-16273/com.mypractice E/—-B Fragment—-: onPause
07-05 11:23:32.099 16273-16273/com.mypractice E/—-A Activity—-: onRestart
07-05 11:23:32.100 16273-16273/com.mypractice E/—-A Activity—-: onStart
07-05 11:23:32.100 16273-16273/com.mypractice E/—-A Fragment—-: onStart
07-05 11:23:32.100 16273-16273/com.mypractice E/—-A Activity—-: onResume
07-05 11:23:32.100 16273-16273/com.mypractice E/—-A Fragment—-: onResume
07-05 11:23:32.515 16273-16273/com.mypractice E/—-B Fragment—-: onStop
07-05 11:23:32.516 16273-16273/com.mypractice E/—-B Fragment—-: onDestroyView
07-05 11:23:32.516 16273-16273/com.mypractice E/—-B Fragment—-: onDestroy
07-05 11:23:32.516 16273-16273/com.mypractice E/—-B Fragment—-: onDetach
⑥Home键回到桌面:
07-05 11:25:56.622 16273-16273/com.mypractice E/—-A Activity—-: onPause
07-05 11:25:56.623 16273-16273/com.mypractice E/—-A Fragment—-: onPause
07-05 11:51:26.861 6921-6921/kj.com.mypractice E/—-A Fragment—-: onSaveInstanceState
07-05 11:25:57.252 16273-16273/com.mypractice E/—-A Activity—-: onStop
07-05 11:25:57.252 16273-16273/com.mypractice E/—-A Fragment—-: onStop
⑦Home键之后返回应用:
07-05 11:26:48.603 16273-16273/com.mypractice E/—-A Activity—-: onRestart
07-05 11:26:48.603 16273-16273/com.mypractice E/—-A Activity—-: onStart
07-05 11:26:48.603 16273-16273/com.mypractice E/—-A Fragment—-: onStart
07-05 11:26:48.603 16273-16273/com.mypractice E/—-A Activity—-: onResume
07-05 11:26:48.603 16273-16273/com.mypractice E/—-A Fragment—-: onResume
⑧退出Activity(即退出Fragment):
07-05 11:28:17.679 16273-16273/com.mypractice E/—-A Activity—-: onPause
07-05 11:28:17.679 16273-16273/com.mypractice E/—-A Fragment—-: onPause
07-05 11:28:18.109 16273-16273/com.mypractice E/—-A Activity—-: onStop
07-05 11:28:18.110 16273-16273/com.mypractice E/—-A Fragment—-: onStop
07-05 11:28:18.110 16273-16273/com.mypractice E/—-A Activity—-: onDestroy
07-05 11:28:18.110 16273-16273/com.mypractice E/—-A Fragment—-: onDestroyView
07-05 11:28:18.111 16273-16273/com.mypractice E/—-A Fragment—-: onDestroy
07-05 11:28:18.111 16273-16273/com.mypractice E/—-A Fragment—-: onDetach
补充:两Activity跳转生命周期方法执行顺序:
① A –> B
07-05 11:32:23.460 27917-27917/kj.com.mypractice E/—-A Activity—-: onPause
07-05 11:32:23.513 27917-27917/kj.com.mypractice E/—-B Activity—-: onCreate
07-05 11:32:23.513 27917-27917/kj.com.mypractice E/—-B Activity—-: onStart
07-05 11:32:23.513 27917-27917/kj.com.mypractice E/—-B Activity—-: onResume
07-05 11:32:23.953 27917-27917/kj.com.mypractice E/—-A Activity—-: onStop
② B –> A
07-05 11:37:45.538 27917-27917/kj.com.mypractice E/—-B Activity—-: onPause
07-05 11:37:45.547 27917-27917/kj.com.mypractice E/—-A Activity—-: onRestart
07-05 11:37:45.547 27917-27917/kj.com.mypractice E/—-A Activity—-: onStart
07-05 11:37:45.547 27917-27917/kj.com.mypractice E/—-A Activity—-: onResume
07-05 11:37:45.976 27917-27917/kj.com.mypractice E/—-B Activity—-: onStop
07-05 11:37:45.976 27917-27917/kj.com.mypractice E/—-B Activity—-: onDestroy
注释:
异常终止的情况(注意是可能,并不是一定):
系统内存不足时
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/170700.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...