/FragmentLifecycleExample

This repository focuses on understanding the Fragment Lifecycle in Android

Primary LanguageKotlin

FragmentLifecycleExample

This repository focuses on understanding the Fragment Lifecycle in Android

Fragment Lifecycle Diagram

Fragment_Lifecycle.jpg

Image Credits: Google Android Developer Documentation

Explanation

The following are the methods that are present in the Activity Lifecycle:

Method Explanation
onAttach() onAttach() is called when the Fragment is attached to an Activity. It is called the first time when it is create
onCreate() onCreate() is called when the Fragment is created the first time after onAttach().

If your fragment needs to receive arguments from its hosting activity, you can access and process those arguments in the onCreate() method.

NOTE:
This method is not primarily related to UI operations or view creation. It's more about preparing the internal state of the fragment and performing tasks that don't require the fragment's view to be available
onCreateView() onCreateView() is called whenever there is a need to create a view. For e.g. During initialisation, and when coming back to the fragment from other fragment
onViewCreated() This method is called when the view has been created but the user still cannot interact with the view at this stage.

This is also called - During initialisation, and when coming back to the fragment from other fragment

NOTE:
Always setup onClickListener in the onViewCreated() method as it is a widely accepted convention and common practice
onViewRestored() onViewRestored() called when the fragment's view hierarchy has been restored, typically after the fragment has been recreated due to a configuration change (e.g., screen rotation, language change)

This method allows you to restore the state of the fragment's view or its UI components
onStart() onStart() is called when the fragment becomes visible to the user, but it is not yet in the foreground. This means that the fragment is about to become partially visible, but it's not yet actively interacting with the user.

The onStart() method is called after the fragment's onAttach(), onCreate() and onCreateView() methods have been executed.

Here, after the hosted activity's onStart() and onResume() both are called, then only the fragment's onStart() is called followed by onResume()

Common Use Cases:
  1. Registering and Unregistering listeners or observers,
  2. Preparing data,
  3. Performing other setup activities that are necessary when the fragment is just about to become visible
onResume() onResume() is called after the onStart() method. It signifies that the fragment is now in the foreground, visible to the user, and ready to receive user input and interaction.

Common Use Cases:
  1. Starting animations,
  2. Refreshing data from a data source,
  3. Updating the user interface,
  4. Initializing any components that requires user interaction
onPause() onPause() is called when the fragment is about to lose visibility or is partially obscured. This can happen when the user -
  1. Navigates to another fragment or activity
  2. Or when the fragment goes into the background due to some other event
At this point, the fragment is still partially visible but no longer actively interacting with the user.

Common Use Cases:
  1. Pause animations,
  2. Release resources,
  3. Stop ongoing tasks, or
  4. Save the current state of the fragment in preparation for it potentially being stopped or destroyed
onStop() onStop() is called when the fragment is no longer visible, and it is not actively running. This usually occurs when the user navigates away from the fragment or when the fragment is replaced by another fragment or activity.

The fragment is not visible on the screen at this stage.

Common Use Cases:
  1. Perform cleanup tasks,
  2. Release resources that are no longer needed, or
  3. Stop background processes associated with the fragment.
onSaveInstanceState() onSaveInstanceState() is a callback method in Android that allows you to save the state of a fragment or an activity when it might be destroyed and later recreated, such as during a configuration change (e.g., screen rotation, language change) or when the system reclaims resources.

Before API 28, onSaveInstanceState() is called before the onStop() Callback
On API 28 & above, onSaveInstanceState() is called after the onStop() Callback
onDestroyView() onDestroyView() is called when the view associated with the fragment is being destroyed.This is typically triggered when the fragment is no longer visible, such as when it is removed from the screen or replaced by another fragment.

At this stage, the fragment's view hierarchy is dismantled, and the view is removed from the fragment, making it inaccessible. The fragment itself is still retained in memory unless it's explicitly removed or the hosting activity is destroyed.

Common Use Cases:
  1. Release references to views, resources, or other objects that were associated with the fragment's view. It's an appropriate place to clean up UI-related resources and perform any necessary teardown
onDestroy() onDestroy() is called when the fragment is being destroyed. This can happen in various scenarios, such as -
  1. When the hosting activity is being destroyed or
  2. When the fragment is removed using a FragmentTransaction

At this point, the fragment is at the end of its lifecycle. The fragment and its associated view are being destroyed, and it is no longer retained in memory.

Common Use Cases:
  1. Release any remaining resources, unregister listeners, and
  2. Perform any final cleanup that is necessary before the fragment is completely removed from memory

Author

Rajit Deb