Fragment = Fragment = Fragment = Fragment = Fragment = Fragment = Fragment = Fragment = Fragment Stop gossiping and return to the truth. Let’s Talk Android!

In order to fully understand the life cycle of a Fragment, we need to understand the state of the Fragment. This time we will cover the state of the Fragment. Fragment has eight operating states, which are:

  • Connection (attach)Copy the code
  • Initialization (init)Copy the code
  • Start (start)Copy the code
  • Run (running)Copy the code
  • Stop (pause)Copy the code
  • Stop (stop)Copy the code
  • Destruction (destroy)Copy the code
  • Disconnect (detach)Copy the code

The Fragment state is only two more states than the Activity state. The other states are exactly the same. Next, we introduce each of these eight states.

Connection (attach)

In this state, fragments are added to the Activity and connected together, creating a connection between them. This state corresponds to the onAttach method in the lifecycle. When the Fragment is in this state, the onAttach method is called back.

Initialization (init)

This state is used to initialize fragment-related tasks, such as loading controls in the layout. This state corresponds to the onCreate and onCreateView methods in the lifecycle. When the Fragment is in this state, the onCreate and onCreateView methods are called back, so we can do some Fragment initiality-related work in these methods, but most of the initiality-related work is done in the onCreateView method.

Start (start)

In this state, the Fragment is loaded. This state corresponds to the onStart method in the lifecycle. When the Fragment is in this state, the onStart method is called back.

Run (running)

In this state, the Fragment gets focus and we can operate on the Fragment. This state corresponds to the onResume method in the lifecycle. When the Fragment is in this state, the onResume method is called back. So we can do things in this method that respond to the Fragment operation, such as having the Fragment communicate with the Activity.

Stop (pause)

In this state, we can still see the Fragment, but we can’t get focus in the Fragment. If we need to save useful data for when the Fragment runs again, we can do it here. This state corresponds to the onPause method in the lifecycle. When the Fragment is in this state, onPause is called back. So we can store the Fragment data in this method.

Stop (stop)

In this state, the Fragment is removed until we can’t see it anymore. This state corresponds to the onStop and onDestroyView methods in the lifecycle. When the Fragment is in this state, the onStop and onDestroyView methods are called back in turn.

Destruction (destroy)

Resources associated with the Fragment are released in this state. This state corresponds to the onDestroy method in the lifecycle. When the Fragment is in this state, the onDestroy method is called back. So we can do something about freeing resources in this method.

Disconnect (detach)

In this state, the Fragment is removed from the Activity, the connection between them is disconnected, and the connection between them is removed. This state corresponds to the onDetach method in the lifecycle. When the Fragment is in this state, the onDetach method is called back.

Fragment: Fragment: Fragment: Fragment: Fragment: Fragment: Fragment: Fragment If you have trouble understanding the Fragment state, you can use the Activity state to understand the Fragment state, because they are very similar. In addition, the states I introduce here are different from the official states, which are only three: Run, pause, and Stop. By contrast, the run, pause, and stop in the official state is the same as the run, pause, and stop in our introduction. We have introduced five more states than the official ones, which we have added to make it easier for you to understand. After adding these states, each state will correspond to one or two callback methods to help you understand how to use different callback methods. In terms of the accuracy of knowledge, we should take the official content as the criterion.

Last but not least, I have some personal opinions: I feel that the state given by the official is incomplete. It is run directly without connection and initialization, which is obviously unreasonable. Of course, the authorities may have other reasons. This is also my opinion, I also mentioned in the introduction of the state of the Activity, welcome to discuss.

Fragment Fragment Fragment Fragment Fragment Fragment Fragment Fragment Fragment Fragment Fragment Fragment Fragment Fragment Fragment Fragment Fragment Fragment Fragment