40

I want to start an Activity (SECOND Activity) from another Activity (FIRST Activity) by calling startActivity(intent). Before I actually do it, I found people say that the lifecycle methods are called in the following order:

FIRST Activity onCreate
FIRST Activity onStart
FIRST Activity onResume
FIRST Activity onPause
SECOND Activity onCreate
SECOND Activity onStart
SECOND Activity onResume
FIRST Activity onStop

Is the SECOND Activity onResume always called before FIRST Acitivity onStop? I thought

FIRST Activity onPause
FIRST Activity onStop
SECOND Activity onCreate
SECOND Activity onStart
SECOND Activity onResume

will be called, but it seems not.

Also, if I just switch between two activities,

FIRST Activity onPause
SECOND Activity onRestart
SECOND Activity onStart
SECOND Activity onResume
FIRST Activity onStop

Are the methods always called in this order?

3 Answers 3

58

Suppose There are two activities FirstActivity and SecondActivity.

Then this order will always remain same everytime.

// when you start FirstActivity

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

when you start SecondActivity using startActivity(new Intent(FirstActivity.this, SecondActivity.class))

(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

// when you press back button on 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

 Note: 

 (1) OnPause() will be called first when you navigate to any other activity.
 
 (2) OnStop() will be called when activity is no longer Visible on screen.
1
  • 1
    Best explanation so far! Sep 11, 2022 at 13:07
45

According to the documentation, SECOND.onResume() is supposed to be called before FIRST.onStop() https://developer.android.com/guide/components/activities/activity-lifecycle#coordinating-activities (Coordinating activities section)

3
  • But bear in mind, some of the methods may never be called like onStop(). Read documentation of each method carefully before implementing your code.
    – OneWorld
    Aug 16, 2013 at 8:01
  • @OneWorld Do you have any sources for that? I can't find anywhere saying that onStop() may never be called. I know that onDestroy() will not be called if the process is in the background and the system decides to kill it without calling all the methods, but can't find any such statements about onStop().
    – Thorbear
    Dec 4, 2016 at 13:58
  • @Thorbear FIRST.onStop will not be called if SECOND does not cover the whole screen and so FIRST stays paused and never gets to the stopped state. Also, link appears to be dead. May 25, 2017 at 15:00
-2

First opens Activity: onCreate(),OnStart(),onResume()

User Clicks backButton(): onPause(),onStop(),onDestroy()

Navigating to another screen:

First Screen:: onPause(),onStop()

Second Screen: onCreate(),OnStart(),onResume()

Presses Backbutton in Activity2:

Second Screen: onPause(),onStop() ,onDestroy()

First Screen: onRestart(), onStart(), onResume()

User locks the device: onPause(),onStop()

Again Opens lock: onRestart(), onStart(), onResume()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.