4 years ago (2015-06-15)  Hundred Encyclopedias Software development |   First to comment  8 
post score 0 times, average 0.0

1 two charts of life cycle

Fragment life cycle diagram:

  android-fragment lifecycle

Fragment and Activity life cycle comparison chart:

Detailed AndroidFragment life cycle

2 Life cycle analysis

1. When a fragment is created, it goes through the following states.

  • onAttach()
  • onCreate()
  • onCreateView()
  • onActivityCreated()

2. When this fragment is visible to the user, it goes through the following states.

  • onStart()
  • onResume()

3. When this fragment enters "background mode", it will experience the following state.

  • onPause()
  • onStop()

4. When this fragment is destroyed (or the activity holding it is destroyed), it will experience the following state.

  • onPause()
  • onStop()
  • onDestroyView()
  • onDestroy() // originally missed this callback, thanks to xiangxue336.
  • onDetach()

5. Just like activitie, in the following state, you can use the Bundle object to save a fragment of the object.

  • onCreate()
  • onCreateView()
  • onActivityCreated()

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!

Post comment

Style

No Comment

登录

Forget password?

您也可以使用第三方帐号快捷登录

切换登录

注册

TW