This repository focuses on understanding the Fragment Lifecycle in Android
Image Credits: Google Android Developer Documentation
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:
|
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:
|
onPause() | onPause() is called when the fragment is about to lose visibility or is partially obscured. This can happen when the user -
Common Use Cases:
|
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:
|
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() CallbackOn 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:
|
onDestroy() | onDestroy() is called when the fragment is being destroyed. This can happen in various scenarios, such as -
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:
|
Rajit Deb