Sitemap
Omkar S Sawant

Omkar S Sawant

Hii , I am Omkar and am a FullStack Developer , bringing ideas to life with passion

Android Fundamentals : Activity LifeCycle

Know the life and Activity and what you should be aware of

3 min readJan 28, 2022

--

The activity is one of the android component and goes through different lifecycle states.In this article we will be learning about different lifecycle states and what are best practices you should know.

CREATION OF ACTIVITY

When the android OS complete’s the creation of activity the onCreate() method is called of the activity and the activity is in CREATED STATE .

After creation the activity prepares itself when the activity is ready the onStart() method of the activity is called by android OS and the activity is in STARTED STATE.

After starting the activity is visible to user and is ready to interact means the user can view click or respond to respective views.the onResume() method of the activity is called and the activity goes into RESUMED STATE.

DESTRUCTION OF ACTIVITY

When the user presses back button or wishes to navigate back.the onPause() method of the activity is called and the activity goes into PAUSED STATE.In this state the activity is visible to user , but the user cannot interact with activity.E.g : When the activity goes in recents screen.

After the onPause() callback the onStop() callback of the activity is called .the activity goes in the STOPPED state.where the user cannot see the activity consequently cannot interact with it.

After onStop() the final callback onDestroy() is called where the activity is in DESTROYED STATE. here no longer the activity Exists.

Thing to Consider While Dealing with Activities

Activity Destruction lifecycle state when pressed :

BACK button

PAUSED -> STOPPED -> DESTROYED

when back button is pressed the states go sequentially.

now,when user reopens the app

Get Omkar S Sawant’s stories in your inbox

Join Medium for free to get updates from this writer.

CREATED -> STARTED -> RESUMED

HOME button

PAUSED -> STOPPED

when the home button is pressed the activity is not destroyed .

now,when user reopens the app

RESTARTED ->STARTED -> RESUMED

RECENTS screen

PAUSED

when user reopens app from recents screen

RESUMED

Why Never do intensive work in PAUSED STATE

when the activity goes in the paused state after that if there another activity coming into the action ,after calling the resume method of the another activity the onStop() method is called.so , here it completes the onPause() callback which can cause ANR.

To avoid this intensive tasks are performed in onStop() as it is called after another activity is in action.

⚠️ System Kill

If android component consumes more memory and does not have priority this android component can be killed by system.No worries for Foreground components they are very less likely to be killed . We need to be careful with background components they have higher chance to be killed due to memory constrains.Note : When System kill activity the onDestroy() may not be called by OS.

video tutorial

Conclusion

This is all you need to know about activity lifecycle .

🔗 Connect with me :

I believe you are a great developer, because you read a detailed blog fully, and I will be too happy to have you. click on the below link to connect with me . https://www.linkedin.com/in/omkar-sawant-dev/

--

--

Omkar S Sawant
Omkar S Sawant