1 two charts of life cycle
Fragment life cycle diagram:
Fragment and Activity life cycle comparison chart:
2 Life cycle analysis
1. When a fragment is created, it goes through the following states.
2. When this fragment is visible to the user, it goes through the following states.
3. When this fragment enters "background mode", it will experience the following state.
4. When this fragment is destroyed (or the activity holding it is destroyed), it will experience the following state.
- onDestroy() // originally missed this callback, thanks to xiangxue336.
5. Just like activitie, in the following state, you can use the Bundle object to save a fragment of the object.
6. Most of the states of the fragments are similar to activitie, but the fragment has some new states.
- onAttached() - called when the fragment is added to the activity (in this method you can get the activity).
- onCreateView() - This method is called when the activity wants the layout of the fragment. Fragment creates its own layout in it.
- onActivityCreated() - This method is called after the activity's onCreated() method returns
- onDestroyView() - This method is called when the view in the fragment is removed.
- onDetach() - This method is called when the fragment is separated from the activity.
Once the activity is in the resumed state (that is, the running state), you are free to add and remove fragments.Therefore, only when the activity is in the resumed state, the fragment's life cycle can run independently, and other times depend on the life cycle of the activity.
This article is translated from http://www.cnblogs.com/purediy/p/3276545.html, in order to respect the author's labor achievements, reproduced please indicate the source!Website link: https://wangbaiyuan.cn/en/android-fragment-2.html!