Skip to content

rajitdeb/ActivityLifecycleExample

Repository files navigation

ActivityLifecycleExample

Understanding in depth about Android Activity Lifecycle

Activity Lifecycle Diagram

Activity_Lifecycle.jpg

Image Credits: Google Android Developer Documentation

Explanation

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:
  1. Variable Initialization, and
  2. View Creation
NOTE: User still doesn't see the UI while the App's State is onCreate()
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:
  1. It flows from onStart(). Basically, onStart() -> onResume()
  2. When the User visits activity again after onPause() or onStop() was called
NOTE:
  1. Here, the Activity is visible to the User and the User can INTERACT with it
  2. And it stays in this state, until another Activity comes in the Foreground (basically displayed over it)
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:
  1. onStop() is called when the Activity is no longer visible at all [No dialogs or any element is visible to the user]
  2. Difference between onPause() and onStop():
    • Both onPause() and onStop() are called one after another
    • If the user gets any notification dialog or any other dialog, that means the rest of the activity ui is still visible, in that case onPause() state is present
    • But if the onStop() is called, it is sure that none of the UI elements of the activity are visible to the User
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:
  1. The User exits the app completely [By back press button]
  2. finish() is called from the Activity
  3. The User removes the app from the background
  4. The Android OS decides to kill your Activity, to free up more memory
  5. When there is a GLOBAL CONFIGURATION CHANGE (such as Screen Rotation) where the screen needs to be recreated with a different UI or configuration
NOTE: [SPECIAL CASE]
  1. onStop() and onDestroy() might not get called in special cases, so we should keep that in mind
  2. 2. And always release resources or save the state in onPause()

Author

Rajit Deb

About

Understanding in depth about Android Activity Lifecycle

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages