This is the 26th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

Fragment represents a behavior or part of the user interface in an Activity. Multiple fragments can be combined into one Activity to build multi-window UI effects, and multiple activities can reuse the same Fragment. Although a Fragment is a modular part of an Activity, it has its own life cycle and can be added and removed freely while the Activity is running. However, fragments must be used within an Activity, and their life cycle is directly affected by the Activity life cycle bound to them.

The life cycle

Fragment lifecycle methods and activities have many similarities. There are several more methods to the Fragment lifecycle than the Activity lifecycle.

onAttach()

OnAttach is called when the Fragment and Activity are already bound. Initialize fragments to get input arguments from getArguments. But you can’t call the getArguments method again when the Fragment is attached to the Activity. So the input information is best executed when the Fragment initializes the binding page.

onCreate

Called when the Fragment is first created, before the bound Activity has been created. You cannot get Activity information in the current state. The Activity can only be retrieved if the state is in onActivityCreated.

onCreateView

OnCreateView returns the View content loaded by the Fragment.

onActivityCreated

The current Fragment bound Activity executes onCreate. In the current state, the Activity is created, so you can get the Activity.

onStart

Fragment is visible on onStart. But the Fragment is not yet in an interactive state. Fragment onStart can be understood as the same state as Activity onStart.

onResume

The Fragment is visible and callable on onResume. The same is true for an Activity’s onResume.

onPause

The same meaning as onPause for an Activity. Usually in the background.

onDestoryView

Fragment when the content view is destroyed. Fragment State when it is about to end or save. This will separate the onCreateView view from the Fragment. If the Fragment needs to restore the display, a new view is created. This state is called between onStop and onDestory.

onDetach

Fragment and Activity are unbound. OnDetach is the last callback in the Fragment lifecycle where the Fragment is unbound and the view structure and resource information is released.

  1. When you open the screen

onCreate() -> onCreateView() -> onActivityCreated() -> onStart() -> onResume() 2. OnPause () -> onStop() 3. Restart the page onStart() -> onResume() 4. OnPause () -> onStop() -> onDestroyView() -> onDestroy() -> onDetach()

As mentioned above, the Fragment life cycle has an impact on the Activity life cycle bound to it. The Fragment life cycle is also different from the Activity life cycle. The Fragment life cycle is called by the bound Activity, but the Activity life cycle is not. Therefore, the Fragment life cycle is related to activities.

  1. When you open the screen

Fragment onCreate() -> Fragment onCreateView() -> Activity onCreate() -> Fragment onActivityCreated() -> ActivityonStart()-> Fragment onStart() ->Activity onResume() ->Fragment onResume()

  1. Back to the home page

Fragment onPause() -> Activity onPause() -> Fragment onStop()-> Activity onStop()

  1. Reopen the page

Activity onReStart() -> Activity onStart() -> Fragment onStart() -> Activity onResume() ->Fragment onResume()

  1. return

Fragment onPause() -> Activity onPause() ->Fragment onStop() -> Activity onStop() -> Fragment onDestroyView() -> Fragment onDestroy() -> Fragment onDetach() -> Activity onDestroy()

As you can see, life cycle method callbacks are basically Fragment calls followed by Activity calls.

reference

  • Fragment lifecycle