Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - While switching between two activities, the calling order of lifecycle methods of Activity

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?

like image 999
Naetmul Avatar asked May 23 '13 01:05

Naetmul


People also ask

What is the order of the activity lifecycle?

Activity-lifecycle concepts To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of six callbacks: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() . The system invokes each of these callbacks as an activity enters a new state.

Which lifecycle method is called first for a new activity?

Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.

What is Android activity life cycle method?

An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() .


2 Answers

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

like image 155
Y2i Avatar answered Oct 22 '22 03:10

Y2i


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. 
like image 33
Samir Mangroliya Avatar answered Oct 22 '22 02:10

Samir Mangroliya