Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android activity lifecycle: state order when new activity starts

If I launch Activity2 from Activity1 by this way: startActivity(Activity2); what executes first: onStop() (Activity1) or onStart() (Activity2) ?

Do they work simultaneously or in turn? If one after another, what is first?

So in general : what is the activity's state order when first activity starts second, if this order exists?

like image 925
Ilya Blokh Avatar asked Apr 01 '12 09:04

Ilya Blokh


People also ask

What is the order of the activity lifecycle?

An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() . The system invokes each of these callbacks as an activity enters a new state.

Which activity state is attained when a new activity?

When the Activity finally gets rendered on the screen, onResume() method is invoked. At this point, the Activity is in the active state and is interacting with the user. If the activity loses its focus and is only partially visible to the user, it enters the paused state.

Which lifecycle method is called first for a new activity?

onCreate() You must implement this callback, which fires when the system first creates the activity. On activity creation, the activity enters the Created state.


2 Answers

enter image description here

when I have checked it by programmatically its following all steps and easy to understand

like image 74
Vivek Hande Avatar answered Sep 18 '22 11:09

Vivek Hande


Let Say Activity A is starting Activity B by Calling StartActivity(Intent) method then lifecycle call be like this :-

  • A onCreate()
  • A onStart()
  • A onResume()

Now Button click for startActivity(intent)

  • A onPause()

  • B onCreate()

  • B onStart()

  • B onResume()

  • A onStop()

..... If you press back button from Activity B then lifeCycle call will be .....

  • B onPause()

  • A onRestart()

  • A onStart()

  • A onResume()

  • B onStop()
  • B onDestory()

Now one more scenario "From Activity A start Activity B by calling StartActivity(Intent) on button click and use finish() method inside onstart() method on Activity B"

  • A onPause()

  • B onCreate()

  • B onStart()

  • A onResume()

  • B onStop()

  • B onDestory()

like image 42
SAURABH_12 Avatar answered Sep 21 '22 11:09

SAURABH_12