Activity Lifecycle
To let Android System interact with Application different methods are defined by the system which communicate different states of the activity to the application. Those methods/states are called as Lifecycle Callbacks.
Activity can go into different combination of these states. In the code each callback is used to understand the state in which the activity is in and accordingly perform different operations.
Let’s discuss different activity callbacks and what each of them represent in real life scenarios.
You will find each section explained with the 3 labels. Let us see what each label represents:

onCreate()
Activity can go into different combination of these states. In the code each callback is used to understand the state in which the activity is in and accordingly perform different operations.
Let’s discuss different activity callbacks and what each of them represent in real life scenarios.
You will find each section explained with the 3 labels. Let us see what each label represents:
- Called: It will tell you when is this method called.
- Implement: What should be implemented in this method.
- State: What is the state of activity from user's perspective.
- Called: This is the first callback received in Activity which is called as soon as the system creates the Activity.
- Implement: Used to initialize essential class objects required in the activity, set the view to be shown in the activity.
- State: Activity is still not visible to the user.
- Called: Just after onCreate() is completed this method is called.
- Implement: Use to initialise the code that maintains UI.
- State: Activity becomes visible to the user but is not usable (user cannot interact with any UI elements)
- Called: OnStart and onResume are called on quick succession.
- Implement: Enable functionalities which require activity to be in foreground, active state.
- State: Activity is in foreground, and interact-able to the user now.
- Called: User is about to leave the activity (not always necessary though). Can be temporarily moved away.
- Implement: Disable functionalities which are not required by the user. In easy words disable all the functionalities enabled in onResume method. onResume and onPause are corresponding lifecycle events. Note do not perform long running operations in this method.
- State: Activity is still visible, but users cannot interact with it. Possible reason: multi-window mode, or semi-transparent activity (such as a dialog) is visible.
- Called: User has left the activity, or any other activity fully visible is shown.
- Implement: Release, clean up resources which were required to keep activity functionality running. Use this method to perform long running operations (like save user state, etc)
- State: Activity is not visible, still in the memory but not attached to the window manager.
- Called: Activity is being destroyed and the same instance will not be used further.
- Implement: Clean up all the remaining resources which were not yet destroyed.
- State: Activity instance will soon be removed from the memory. Possible reason “user pressed back button”, “finish” or “user rotated the screen (i.e configuration change)”.
- Called: After onStop(), activity becomes visible to the user again.
- Implement: Some functionalities which were disable in onStop can be initialized here so that before the activity becomes visible to user data can be fetched and shown.
- State: Activity might be visible but cannot be interacted.
Resources
https://developer.android.com/guide/components/activities/activity-lifecycle
Comments
Post a Comment