Understanding in depth about Android Activity Lifecycle
Image Credits: Google Android Developer Documentation
The following are the methods that are present in the Activity Lifecycle:
Method | Explanation |
---|---|
onCreate() | onCreate() is called on First-time Launch or when activity is destroyed and recreated. This method is where we do:
|
onStart() | onStart() is called when the activity is visited after being sent to background also when it flows from onCreate() Basically, onCreate() -> onStart() NOTE: Here, User can see the Views but can't INTERACT with it. |
onResume() | onResume() is called when:
|
onPause() | IMPORTANT: If we need to release resources, we always do it in onPause() to ensure they are release every single time. onPause() is called when another Activity is visible in the foreground over the activity NOTE: Not only any Activity but also Ui Elements like Dialog From here, if the User comes back to the Activity , then the flow jumps to onPause() -> onResume() |
onStop() | onStop() is called when the user leaves the app and goes to another app, but the app remains in the background Lifecycle Flow: onStop() -> [If users comes back to the activity] onRestart() NOTE:
|
onRestart() | onRestart() is called every time when the user visits the app after onStop() was called Flow: onStop() -> onRestart() NOTE: onRestart() is called, when the user returned to the Activity , after it went to onStop() state |
onDestroy() | onDestroy() is called when:
|
Rajit Deb