Lifecycle methods of Android Activity — Scenario based Question

Balaji S B
Proggy Blast
Published in
2 min readDec 14, 2020
Interview Preparation Guide Android — Proggyblast

Most of the interviewers are asking the lifecycle of the activity in android. But that is not the only thing to remember. Most of the time they will ask scenario based questions. Here in this article, we can discuss about one of the most and frequently asked scenario on android activity. Let’s dive into it.

Scenario 1 :

What are the lifecycle methods will get call when we are navigating from Activity A to Activity B?

Consider we have two activities. One is activity A and another one is activity B. Activity A has one button. By pressing the button it will navigate to activity B. Here we can see what are the lifecycle methods will get call.

When activity A is rendered first it will call onCreate() method. Then it will call onStart() method. Then once the user is able to interact with the UI it will call onResume() method. When the user is pressing the navigate button it will open activity B.

Now onPause() method of activity A will get call. Then activity B’s onCreate() mehtod followed by onStart() and onResume() is called. Then activity A’s onStop() method will trigger.

Here is the callback orders

Activity A -> onCreate() , onStart(), onResume(), onPause()

Activity B -> onCreate(), onStart(), onResume()

Activity A -> onStop()

Scenario 2:

What are the lifecycle methods will get call when pressing the back button from Activity B to Activity A?

When we press the back button of Activity B the onPause() method of activity B is triggered. Then activity A’s onRestart() method is called and followed by onStart() and onResume() of activity A is called. After that activity B’s onStop() will get call and followed by onDestroy() will trigger.

Here is the callback orders

Activity B -> onPause()

Activity A -> onRestart(), onStart(), onResume()

Activity B -> onStop(), onDestroy()

Scenario 3:

What are the lifecycle methods will get call when pressing the home button from Activity A?

When we are pressing the home button from activity A the onPause() and onStop() methods will trigger. When we open the app again from recent apps the methods onRestart(), onStart() and onResume() will trigger.

Here is the callback methods

Pressing home button,

Activity A -> onPause(), onStop()

Resuming from recent apps,

Activity A -> onRestart(), onStart(), onResume()

Conclusion :

So from this article you come to know what are the callback methods getting called while navigating from one activity to another activity. Please do clap (👏) if you like this post. Thanks and Cheers.

#android #proggyblast #interviewquestion #Balaji-SB Balaji S B Proggy Blast

--

--

Balaji S B
Proggy Blast

Technical Lead @ Happeist Minds Technologies Pvt Ltd