Fragment Lifecycle

Just like Activities, Fragments also manages its own lifecycle. Though these lifecycle callbacks are a bit different than activity’s callbacks they exist for the similar reason as that of activity’s callback.

Fragment manages two different lifecycles. One for the fragment instance and other for the view shown in the fragment. Having different lifecycle for fragment’s view is useful in the situations where the work should be performed only when view exists.

Points to note:

  • Fragment lifecycle call-backs are similar to Activity’s lifecycle. More important they are dependent on Activity’ state.
  • Fragment manages 2 different lifecycles - Instance and View.
  • Fragment callbacks are: onAttach, onCreate, onCreateView, onViewCreated, onViewStateRestored, onStart, onResume, onPause, onStop, onDestroyView, onDestroy, onDetach
  • Fragment state is saved before onStop for API level less than 28 while in API level 28 and above its saved after onStop method. i.e In API level 28 or above devices its safe to perform fragment transaction onStop method.
  • onDetach/onAttach callbacks are completely different then attach()/detach() methods of the FragmentTransaction class. Later methods will result in creation/destruction of fragment’s view while prior methods represents that fragment instance is created/destroyed.

onAttach

  • Called: This is the first callback method. Called when fragment is first attached to its context (Activity or parent Fragment)
  • Implement: Can register here to know whether its child fragments are attached.
  • Fragment instance is added to FragmentManager. Not visible to the user.

onCreate

  • Called: Called just after onAttach method.
  • Implement: Can initialise object required at fragment level. Should not try to access any things related to the View.
  • Not visible to the user.

onCreateView

  • Called: Called to have the fragment instantiate its user interface view
  • Implement: Return the layout or View which will be shown in the Fragment. Can also return NULL if not view is to be shown.
  • Not visible to the user.

onViewCreated

  • Called: Called just after onCreateView has returned.
  • Implement: Setup initial state of the View (e.g click listener, touch listener, view update observers , etc).
  • Fragment View hierarchy still not attached to its parent. Not visible to the user.

onViewStateRestored

  • Called: Called when all saved state has been restored into the view hierarchy of the fragment
  • Implement: Initialize the fragment state the saved state of the view component. E.g. initialise a variable tracking whether the checkbox component is checked or not
  • Not visible to the user.

onStart

  • Called: When fragment’s view is visible and available to operate on. E.g can add child fragment in this callback.
  • Implement: Used to initialize lifecycle aware components.
  • Fragment’s view is visible and might have related animation and transitions running. User cannot interact with it. Generally tied with its parent Activity’s onStart method.

onResume

  • Called: Called once all the animation and transitions are completed.
  • Implement: Used to start the functionalities which need the screen to be inter-actable to the user
  • Fragment’s view is visible and is inter-actable i.e user can use the UI components visible on the screen.

onPause

  • Called: User have started to leave the fragment though the view is still visible.
  • Implement: Stop the functionalities which were started in onResume
  • Fragment’s view is visible but user cannot interact with it.

onStop

  • Called: Called when fragment is not longer visible. Has fragment animation and transition running.
  • Implement: Reset the objects initialised in onStart method. Last point where FragmentTransaction can be performed but only for API 28 and above. For lower it would result in crash.
  • Fragment is not visible to the user.

onDestoryView

  • Called: Called when fragment’s view is detached from the window. This represents end of the fragment’s view lifecycle.
  • Implement: Clean up any view related references. Do not access any view references. So the view can be garbage collected.

onDestroy

  • Called: When fragment is removed or its hosting component is destroyed/removed. This represents end of the fragment lifecycle.
  • Implement: Clean up any member variables to allow them to get garbage collected.

onDetach

  • Called: When fragment is no longed attached to its activity i.e the reference of the fragment is removed from the memory.
  • Implement: Remove any attached listener.

Source: https://developer.android.com/guide/fragments/lifecycle

Resources

https://developer.android.com/guide/fragments/lifecycle
https://developer.android.com/reference/androidx/fragment/app/Fragmen

Comments

Popular posts from this blog

AndroidManifest

Activity Lifecycle