[Android] fragment and fragment life cycle

fragment and fragment life cycle

fragment

A fragment is a reusable application interface fragment. Similar to activities, fragments have a life cycle and can respond to user input. When a fragment is displayed on the screen, it is always included in the activity's view hierarchy. Because fragments focus on reusability and modularity, multiple fragments can even be hosted simultaneously by a single activity. Each fragment manages its own separate life cycle.


fragment life cycle

Like activities, fragments can be initialized and removed from memory; fragments can appear, disappear, and reappear on the screen throughout their lifetime. In addition, similar to activities, fragments also have a life cycle with multiple states and provide several alternative methods to respond to transitions between them. There are five states in the fragment life cycle, represented by the Lifecycle.State enumeration.

  • INITIALIZED: A new instance of fragment has been instantiated.
  • CREATED: The system has called the first batch of fragment life cycle methods. While the fragment is in this state, its associated views are also created.
  • STARTED: The fragment is visible on the screen but has no focus, which means it cannot respond to user input.
  • RESUMED: The fragment is visible and has focus.
  • DESTROYED: The fragment object has been deinstantiated.

In addition, similar to activities, the Fragment class also provides a variety of alternative methods to respond to life cycle events.

  • onCreate():fragment has been instantiated and is in CREATED state. However, its corresponding view has not yet been created.
  • onCreateView(): This method can be used to inflate the layout. The fragment has entered the CREATED state.
  • onViewCreated(): This method is called after the view is created. In this method, you typically bind a specific view to a property by calling findViewById().
  • onStart(): The fragment has entered the STARTED state.
  • onResume(): The fragment has entered the RESUMED state and now has focus (can respond to user input).
  • onPause(): The fragment has re-entered the STARTED state. The corresponding interface is visible to the user.
  • onStop():fragment has re-entered the CREATED state. The object is instantiated, but it is no longer displayed on the screen.
  • onDestroyView(): This method is called before the fragment enters the DESTROYED state. The view has been removed from memory, but the fragment object still exists.
  • onDestroy():fragment enters DESTROYED state.
    The following diagram summarizes the fragment life cycle and the transitions between states.

74470aacefa170bd.png

Lifecycle states and callback methods are very similar to those used for activities. But please note onCreate()the difference in approach. With an activity, you can inflate layouts and bound views using this method. However, in the fragment lifecycle, the system calls it before creating the view onCreate(), so you cannot inflate the layout here. You can onCreateView()do this in instead. Then, after the view is created, the system calls onViewCreated()the method where you can bind properties to a specific view.

Guess you like

Origin blog.csdn.net/weixin_42473228/article/details/131530587