欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 和 Fragment 生命周期回调执行顺序

程序员文章站 2022-05-14 08:03:58
...

 

目录

1. 问题

2. 记录

2.1 Activity

2.2 Fragment

3. 附录数据

3.1 实验一:

3.2 实验二:


 

1. 问题

最近发现有些时候Fragment 周期和Activity 周期执行顺序弄混了,有些情况下App 展示会出问题,然后研究一番,简单记录一下回调执行顺序;

 

2. 记录

2.1 Activity

Activity 生命周期:

Android 和 Fragment 生命周期回调执行顺序

 

a. 手机打开APP 并成功创建Activity

I/FragmentHomeActivity: onCreate()

I/FragmentHomeActivity: onAttachFragment()
I/FragmentHomeActivity: onAttachFragment()

I/FragmentHomeActivity: onStart()
I/FragmentHomeActivity: onResume()

I/FragmentHomeActivity: onResumeFragments()
I/FragmentHomeActivity: onAttachedToWindow()

b. activity 切换到后台,回到桌面

I/FragmentHomeActivity: onPause()
I/FragmentHomeActivity: onStop()

I/FragmentHomeActivity: onSaveInstanceState()

c. activity 切换到前台,从桌面回到APP(直接点击APP 图标也是一样的)

I/FragmentHomeActivity: onRestart()
I/FragmentHomeActivity: onStart()
I/FragmentHomeActivity: onResume()

I/FragmentHomeActivity: onResumeFragments()

d. activity 后退,关闭当前页面

I/FragmentHomeActivity: onPause()
I/FragmentHomeActivity: onStop()
I/FragmentHomeActivity: onDestroy()

I/FragmentHomeActivity: onDetachedFromWindow()

2.2 Fragment

Fragment 生命周期:

Android 和 Fragment 生命周期回调执行顺序

a. 手机打开APP,并成功建立Fragment(在Activity 中)

I/AFragment: onAttach()
I/AFragment: onCreate()
I/AFragment: onCreateView()
I/AFragment: onViewCreated()
I/AFragment: onActivityCreated()

I/AFragment: onViewStateRestored()

I/AFragment: onHiddenChanged(true)    // 注意,首次创建会调用2遍,需要结合onResume() 判断
I/AFragment: onHiddenChanged(false)
I/AFragment: onStart()
I/AFragment: onResume()

 b. Fragment 切换到后台,回到桌面

I/AFragment: onPause()
I/AFragment: onStop()

I/AFragment: onSaveInstanceState()

c. Fragment 切换到前台,从桌面回到APP(直接点击APP 图标也是一样的)

I/AFragment: onStart()
I/AFragment: onResume()

d. activity 后退,关闭当前页面(Fragment 销毁)

I/AFragment: onPause()

I/AFragment: onStop()
I/AFragment: onDestroyView()
I/AFragment: onDestroy()
I/AFragment: onDetach()

e. 成功建立AFragment,BFragment,并切换到BFragment

I/AFragment: onAttach()
I/AFragment: onCreate()
I/AFragment: onCreateView()
I/AFragment: onViewCreated()
I/AFragment: onActivityCreated()
I/AFragment: onViewStateRestored()
I/AFragment: onHiddenChanged(true)
I/AFragment: onHiddenChanged(false)
I/AFragment: onStart()
I/AFragment: onResume()

I/AFragment: onHiddenChanged(true) 
I/AFragment: onAttach()
I/AFragment: onCreate()
I/AFragment: onCreateView()
I/AFragment: onViewCreated()
I/AFragment: onActivityCreated()
I/AFragment: onViewStateRestored()
I/AFragment: onHiddenChanged(true)

I/BFragment: onAttach()
I/BFragment: onCreate()
I/BFragment: onCreateView()
I/BFragment: onViewCreated()
I/BFragment: onActivityCreated()
I/BFragment: onViewStateRestored()
I/BFragment: onHiddenChanged(true)

I/AFragment: onHiddenChanged(false)

I/AFragment: onStart()
I/BFragment: onStart()

I/AFragment: onResume()
I/BFragment: onResume()

I/AFragment: onHiddenChanged(true)
I/BFragment: onHiddenChanged(false)

f. 成功建立AFragment,BFragment,并切换到BFragment,在切换到AFragment,最后后退关闭当前Activity(销毁AFragment,BFragment)

I/AFragment: onAttach()
I/AFragment: onCreate()
I/AFragment: onCreateView()
I/AFragment: onViewCreated()
I/AFragment: onActivityCreated()
I/AFragment: onViewStateRestored()
I/AFragment: onHiddenChanged(true)

I/BFragment: onAttach()
I/BFragment: onCreate()
I/BFragment: onCreateView()
I/BFragment: onViewCreated()
I/BFragment: onActivityCreated()
I/BFragment: onViewStateRestored()
I/BFragment: onHiddenChanged(true)

I/AFragment: onHiddenChanged(false)

I/AFragment: onStart()
I/BFragment: onStart()

I/AFragment: onResume()
I/BFragment: onResume()    // 截止到此处向上, AFragment 和BFragment 创建的完整过程

I/AFragment: onHiddenChanged(true)    // 切换到BFragment
I/BFragment: onHiddenChanged(false)

I/BFragment: onHiddenChanged(true)    // 切换到AFragment
I/AFragment: onHiddenChanged(false)

I/AFragment: onPause()    // 从此处向下,AFragment 和BFragment 销毁的完整过程
I/BFragment: onPause()

I/AFragment: onStop()
I/BFragment: onStop()

I/AFragment: onDestroyView()
I/AFragment: onDestroy()
I/AFragment: onDetach()

I/BFragment: onDestroyView()
I/BFragment: onDestroy()
I/BFragment: onDetach()

g. 完整的Activity 创建,AFragment和BFragment 创建,切换BFragment 然后切换到AFragment,最后关闭页面Activity,销毁AFragment和BFragment 如下:

I/FragmentHomeActivity: onCreate()

I/AFragment: onAttach()
I/FragmentHomeActivity: onAttachFragment()
I/AFragment: onCreate()
I/AFragment: onCreateView()
I/AFragment: onViewCreated()
I/AFragment: onActivityCreated()
I/AFragment: onViewStateRestored()
I/AFragment: onHiddenChanged(true)

I/BFragment: onAttach()
I/FragmentHomeActivity: onAttachFragment()
I/BFragment: onCreate()
I/BFragment: onCreateView()
I/BFragment: onViewCreated()
I/BFragment: onActivityCreated()
I/BFragment: onViewStateRestored()
I/BFragment: onHiddenChanged(true)

I/FragmentHomeActivity: onAttachFragment()

I/AFragment: onHiddenChanged(false)

I/AFragment: onStart()
I/BFragment: onStart()
I/FragmentHomeActivity: onStart()

I/FragmentHomeActivity: onResume()
I/AFragment: onResume()
I/BFragment: onResume()

I/FragmentHomeActivity: onResumeFragments()

I/FragmentHomeActivity: onAttachedToWindow()

I/AFragment: onHiddenChanged(true)    // 切换到BFragment
I/BFragment: onHiddenChanged(false)

I/BFragment: onHiddenChanged(true)    // 切换到AFragment
I/AFragment: onHiddenChanged(false)

I/AFragment: onPause()
I/BFragment: onPause()
I/FragmentHomeActivity: onPause()

I/AFragment: onStop()
I/BFragment: onStop()
I/FragmentHomeActivity: onStop()

I/AFragment: onDestroyView()
I/AFragment: onDestroy()
I/AFragment: onDetach()

I/BFragment: onDestroyView()
I/BFragment: onDestroy()
I/BFragment: onDetach()

I/FragmentHomeActivity: onDestroy()
I/FragmentHomeActivity: onDetachedFromWindow()

 

3. 附录数据

自己折腾了折腾,把记录贴下来,后续有问题了继续修正更新;

 

3.1 实验一

有个FragmentHomeActivity 里包含 AFragment|BFragment|CFragment|DFragment,

初始条件:

              1. 不包含CFragment 实例(默认状态);

 

3.2 实验二

有个FragmentHomeActivity 里包含 AFragment|BFragment|CFragment|DFragment,

初始条件:

              1. 不包含CFragment 实例(默认状态); 

              2. 打开开关,包含CFragment;

 

实验记录如下:

## 初始化(无CFragment,默认关闭)

I/FragmentHomeActivity: onCreate()
I/AFragment: onAttach()
I/FragmentHomeActivity: onAttachFragment()
I/AFragment: onCreate()
I/AFragment: onCreateView()
I/AFragment: onViewCreated()
I/AFragment: onActivityCreated()
I/AFragment: onViewStateRestored()
I/AFragment: onHiddenChanged(true)
I/BFragment: onAttach()
I/FragmentHomeActivity: onAttachFragment()
I/BFragment: onCreate()
I/BFragment: onCreateView()
I/BFragment: onViewCreated()
I/BFragment: onActivityCreated()
I/BFragment: onViewStateRestored()
I/BFragment: onHiddenChanged(true)
I/DFragment: onAttach()
I/FragmentHomeActivity: onAttachFragment()
I/DFragment: onCreate()
I/DFragment: onCreateView()
I/DFragment: onViewCreated()
I/DFragment: onActivityCreated()
I/DFragment: onViewStateRestored()
I/DFragment: onHiddenChanged(true)
I/AFragment: onHiddenChanged(false)
I/AFragment: onStart()
I/BFragment: onStart()
I/DFragment: onStart()
I/FragmentHomeActivity: onStart()
I/FragmentHomeActivity: onResume()
I/AFragment: onResume()
I/BFragment: onResume()
I/DFragment: onResume()
I/FragmentHomeActivity: onResumeFragments()
I/FragmentHomeActivity: onAttachedToWindow()

### A -> B, B -> D, D -> A

I/AFragment: onHiddenChanged(true)
I/BFragment: onHiddenChanged(false)

I/BFragment: onHiddenChanged(true)
I/DFragment: onHiddenChanged(false)

I/DFragment: onHiddenChanged(true)
I/AFragment: onHiddenChanged(false)

### 切换到后台

I/AFragment: onPause()
I/BFragment: onPause()
I/DFragment: onPause()
I/FragmentHomeActivity: onPause()
I/AFragment: onStop()
I/BFragment: onStop()
I/DFragment: onStop()
I/FragmentHomeActivity: onStop()
I/AFragment: onSaveInstanceState()
I/BFragment: onSaveInstanceState()
I/DFragment: onSaveInstanceState()
I/FragmentHomeActivity: onSaveInstanceState()

### 回到前台

I/FragmentHomeActivity: onRestart()
I/AFragment: onStart()
I/BFragment: onStart()
I/DFragment: onStart()
I/FragmentHomeActivity: onStart()
I/FragmentHomeActivity: onResume()
I/AFragment: onResume()
I/BFragment: onResume()
I/DFragment: onResume()
I/FragmentHomeActivity: onResumeFragments()

### 关闭页面(销毁Fragment)

I/AFragment: onPause()
I/BFragment: onPause()
I/DFragment: onPause()
I/FragmentHomeActivity: onPause()
I/AFragment: onStop()
I/BFragment: onStop()
I/DFragment: onStop()
I/FragmentHomeActivity: onStop()
I/AFragment: onDestroyView()
I/AFragment: onDestroy()
I/AFragment: onDetach()
I/BFragment: onDestroyView()
I/BFragment: onDestroy()
I/BFragment: onDetach()
I/DFragment: onDestroyView()
I/DFragment: onDestroy()
I/DFragment: onDetach()
I/FragmentHomeActivity: onDestroy()
I/FragmentHomeActivity: onDetachedFromWindow()

## 初始化(无CFragment,默认关闭)

I/FragmentHomeActivity: onCreate()
I/AFragment: onAttach()
I/FragmentHomeActivity: onAttachFragment()
I/AFragment: onCreate()
I/AFragment: onCreateView()
I/AFragment: onViewCreated()
I/AFragment: onActivityCreated()
I/AFragment: onViewStateRestored()
I/AFragment: onHiddenChanged(true)
I/BFragment: onAttach()
I/FragmentHomeActivity: onAttachFragment()
I/BFragment: onCreate()
I/BFragment: onCreateView()
I/BFragment: onViewCreated()
I/BFragment: onActivityCreated()
I/BFragment: onViewStateRestored()
I/BFragment: onHiddenChanged(true)
I/DFragment: onAttach()
I/FragmentHomeActivity: onAttachFragment()
I/DFragment: onCreate()
I/DFragment: onCreateView()
I/DFragment: onViewCreated()
I/DFragment: onActivityCreated()
I/DFragment: onViewStateRestored()
I/DFragment: onHiddenChanged(true)
I/AFragment: onHiddenChanged(false)
I/AFragment: onStart()
I/BFragment: onStart()
I/DFragment: onStart()
I/FragmentHomeActivity: onStart()
I/FragmentHomeActivity: onResume()
I/AFragment: onResume()
I/BFragment: onResume()
I/DFragment: onResume()
I/FragmentHomeActivity: onResumeFragments()
I/FragmentHomeActivity: onAttachedToWindow()

## 初始化(开启CFragment,打开开关,并切换到AFragment)

I/CFragment: onAttach()
I/FragmentHomeActivity: onAttachFragment()
I/CFragment: onCreate()
I/CFragment: onCreateView()
I/CFragment: onViewCreated()
I/CFragment: onActivityCreated()
I/CFragment: onViewStateRestored()
I/CFragment: onStart()
I/CFragment: onResume()
I/CFragment: onHiddenChanged(true)
I/AFragment: onHiddenChanged(true)
I/AFragment: onHiddenChanged(false)

### A -> B, B -> C, C -> D, D -> C, C -> B, B -> A

I/AFragment: onHiddenChanged(true)
I/BFragment: onHiddenChanged(false)
I/BFragment: onHiddenChanged(true)
I/CFragment: onHiddenChanged(false)
I/CFragment: onHiddenChanged(true)
I/DFragment: onHiddenChanged(false)
I/DFragment: onHiddenChanged(true)
I/CFragment: onHiddenChanged(false)
I/CFragment: onHiddenChanged(true)
I/BFragment: onHiddenChanged(false)
I/BFragment: onHiddenChanged(true)
I/AFragment: onHiddenChanged(false)

### 切换到后台

I/AFragment: onPause()
I/BFragment: onPause()
I/DFragment: onPause()
I/CFragment: onPause()
I/FragmentHomeActivity: onPause()
I/AFragment: onStop()
I/BFragment: onStop()
I/DFragment: onStop()
I/CFragment: onStop()
I/FragmentHomeActivity: onStop()
I/AFragment: onSaveInstanceState()
I/BFragment: onSaveInstanceState()
I/DFragment: onSaveInstanceState()
I/CFragment: onSaveInstanceState()
I/FragmentHomeActivity: onSaveInstanceState()

### 回到前台

I/FragmentHomeActivity: onRestart()
I/AFragment: onStart()
I/BFragment: onStart()
I/DFragment: onStart()
I/CFragment: onStart()
I/FragmentHomeActivity: onStart()
I/FragmentHomeActivity: onResume()
I/AFragment: onResume()
I/BFragment: onResume()
I/DFragment: onResume()
I/CFragment: onResume()
I/FragmentHomeActivity: onResumeFragments()

### 关闭页面(销毁Fragment)

I/AFragment: onPause()
I/BFragment: onPause()
I/DFragment: onPause()
I/CFragment: onPause()
I/FragmentHomeActivity: onPause()
I/AFragment: onStop()
I/BFragment: onStop()
I/DFragment: onStop()
I/CFragment: onStop()
I/FragmentHomeActivity: onStop()
I/AFragment: onDestroyView()
I/AFragment: onDestroy()
I/AFragment: onDetach()
I/BFragment: onDestroyView()
I/BFragment: onDestroy()
I/BFragment: onDetach()
I/DFragment: onDestroyView()
I/DFragment: onDestroy()
I/DFragment: onDetach()
I/CFragment: onDestroyView()
I/CFragment: onDestroy()
I/CFragment: onDetach()
I/FragmentHomeActivity: onDestroy()
I/FragmentHomeActivity: onDetachedFromWindow()



 

 

(完)