Understanding Android Lifecycle – Activity and Fragment Lifecycle
Learn about the Activity and Fragment lifecycle in Android. Understand key methods like onCreate(), onStart(), onResume(), and Fragment lifecycle methods like onAttach(), onCreateView(), and more.
In Android, managing the lifecycle of components such as Activities and Fragments is a critical part of app development. The lifecycle methods help manage the app’s behavior based on various system events, such as backgrounding, user interaction, or configuration changes. Understanding these lifecycle methods ensures your app is efficient and responsive.
i) Activity Lifecycle
An Activity is a single screen in an Android application, and it goes through a series of lifecycle stages. The system uses lifecycle methods to transition through these stages, and as a developer, understanding these methods will help manage resources, data, and UI updates efficiently.
Key Activity Lifecycle Methods
a) onCreate()
This method is called when the activity is first created. It is used to initialize components, set the layout, and perform one-time setup actions.
b) onStart()
Called when the activity is becoming visible to the user. This method is invoked after onCreate() and before onResume().
c) onResume()
Called when the activity is in the foreground and interacting with the user. It is used to restart tasks like animations, refresh data, or any ongoing processes.
d) onPause()
This method is called when the system is about to start or resume another activity. It’s used to pause ongoing tasks, release resources, and save any necessary data.
e) onStop()
Called when the activity is no longer visible to the user. This is a good place to release resources that are no longer needed.
f) onRestart()
This method is invoked when the activity is restarted after being stopped. It is called just before onStart().
g) onDestroy()
Called when the activity is about to be destroyed. This is where you should clean up any resources and stop any background tasks.
Activity Lifecycle Flow
i) onCreate() → onStart() → onResume()
When the activity is paused or stopped:
ii) onPause() → onStop()
When the activity is restarted:
iii) onRestart() → onStart() → onResume()
Finally, when the activity is destroyed:
iv) onDestroy()
ii) Fragment Lifecycle
A Fragment is a portion of the activity’s UI or behavior. Since fragments can be dynamically added or removed during runtime, they have their own lifecycle, which is managed by the hosting activity.
Key Fragment Lifecycle Methods
a) onAttach()
This method is called when the fragment is first attached to its host activity. Here you can access the activity's context and perform initial setup.
b) onCreate()
Called to initialize the fragment. This method is used for setting up configurations that do not depend on the fragment’s view.
c) onCreateView()
This method is called to create the fragment’s view. The view is inflated from the layout resource and is used as the UI of the fragment.
d) onActivityCreated()
Called after the host activity’s onCreate() method has been invoked. You can access and interact with the activity’s views here.
e) onStart()
This method is invoked when the fragment becomes visible to the user. It is called after onCreateView() and before onResume().
f) onResume()
This method is called when the fragment is active and in the foreground. Use this to start tasks that need to run while the fragment is visible and interacting with the user.
g) onPause()
Called when the fragment is no longer in the foreground but still partially visible. This method is used to pause ongoing tasks and save data if necessary.
h) onStop()
This method is called when the fragment is no longer visible to the user. It is similar to the onStop() method in activities.
i) onDestroyView()
This method is called when the fragment’s view is being destroyed. Here, you can clean up resources that were tied to the fragment’s view.
j) onDestroy()
Called when the fragment is being destroyed. This is the time to clean up any remaining resources.
k) onDetach()
This is the final method in the fragment lifecycle, called when the fragment is detached from its host activity.
Fragment Lifecycle Flow
i) onAttach() → onCreate() → onCreateView() → onActivityCreated() → onStart() → onResume()
When the fragment is paused or stopped:
ii) onPause() → onStop() → onDestroyView() → onDestroy()
Finally, when the fragment is detached:
iii) onDetach()
iii) Key Differences Between Activity and Fragment Lifecycles
| MethodActivityFragment | ||
| onCreate() | Called when the activity is created | Called when the fragment is created |
| onCreateView() | Not applicable | Called to create the fragment's view |
| onDestroyView() | Not applicable | Called to destroy the fragment's view |
| onPause() | Called when the activity is paused | Called when the fragment is paused |
| onStop() | Called when the activity is stopped | Called when the fragment is stopped |