Basics of android activity lifecycle Coordination

jigar mori
3 min readSep 4, 2020

--

In today’s topic we are going to discuss about activity lifecycle coordination, I have seen so many beginners are making mistakes while they have been asked about the lifecycle event of activity when it switch to another, or back from other activity, so here i am writing an article about it to help android beginners to understand the flow

activity lifecycle diagram

We all have seen the activity lifecycle diagram from official android documentation website, have the basic idea about it. To understand the flow we are going to take two activities, Suppose our First activities name is FirstActivity and it is launched for the first time then it will going to call lifecycle events as below.

(1) OnCreate() -> OnStart() -> OnResume() of FirstActivity will be called

Now if we go to the next activity from first one, Suppose the next activity name is SecondActivity then below lifecycle event will be called for FirstActivity and SecondActivity

(2) OnPause() of FirstActivity will be called and then

(3) OnCreate() -> OnStart() -> OnResume() of SecondActivity will be Called then

(4) OnStop() of FirstActivity will be called

Here is the explanation from android’s official website about Coordinating activities

When one activity starts another, they both experience lifecycle transitions. The first activity stops operating and enters the Paused or Stopped state, while the other activity is created. In case these activities share data saved to disc or elsewhere, it’s important to understand that the first activity is not completely stopped before the second one is created. Rather, the process of starting the second one overlaps with the process of stopping the first one.

The order of lifecycle callbacks is well defined, particularly when the two activities are in the same process (app) and one is starting the other. Here’s the order of operations that occur when Activity A starts Activity B:

  1. Activity A’s onPause() method executes.
  2. Activity B’s onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)
  3. Then, if Activity A is no longer visible on screen, its onStop() method executes.

This predictable sequence of lifecycle callbacks allows you to manage the transition of information from one activity to another.

Now moving forward, what lifecycle event will be called when we move back to the FirstActivity From SecondActivity

(5) OnPause() of SecondActivity will be called then

(6) OnRestart() -> OnStart() -> OnResume() of FirstActivity will be called then

(7) onStop() -> onDestroy() of SecondActivity will be called

So, This is the full lifecycle calling events flow of both of the activities. I hope this will help the android beginners to understand the activity lifecycle event more.

OnPause() will be called first when you navigate to any other activity. OnStop() will be called when activity is no longer Visible on screen.

That’s it for today’s topic, For fragment Lifecycle coordination you can check below article

Happy learning!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

jigar mori
jigar mori

No responses yet

Write a response