Android activity lifecycle: state order when new activity starts - android

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?

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()

According to the documentation, the onStart on Activity2 is called before onStop on Activity1 (or, if you prefer, the os waits onStart on Activity2 to be finished before calling onStop on Activity1).
From http://developer.android.com/guide/topics/fundamentals/activities.html:
The order of lifecycle callbacks is well defined, particularly when the two activities are in the same process and one is starting the other. Here's the order of operations that occur when Activity A starts Acivity B:
Activity A's onPause() method executes.
Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)
Then, if Activity A is no longer visible on screen, its onStop() method executes.

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

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()

Here's the order of operations that occur when Activity A starts Activity B:
Activity A's onPause() method executes.
Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)
Then, if Activity A is no longer visible on screen, its onStop() method executes.
1

The key is understanding how activity is started. When you publish Intent in startActivity() method you just ask system to start this activity. Next system try to start Activity2 and sends message to Activity1. Order is undetermined and can be different in different situations.
Looks like my anwer was wrong for situation when both activities works within this same process (app)
As pointed Daniil Popov: https://developer.android.com/guide/components/activities/activity-lifecycle.html (Coordinating activities section)

When ever we navigate from first activity to second then
onPause() method is called followed by the onStop() and then the method onCreate() of second activity is called followed by onStart() and then onResume().
Also when navigating back to firstactivity by pressing back key
onPause() method of second activity is called followed by the onStop() and then the method onRestart() of first activity is called followed by onStart() and then onResume().

Use Log to post logs to Logcat.
Log.v("STATE", "Pause...and so on");

The order of lifecycle callbacks is well defined, particularly when the two activities are in the same process and one is starting the other. Here's the order of operations that occur when Activity A starts Acivity B:
Activity A's onPause() method executes. Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.) Then, if Activity A is no longer visible on screen, its onStop() method executes.

Related

Example for activity that starts another activity but is still visible

I just read the following from Android Developers docs:
Here's the order of operations that occur when Activity A starts
Activity B:
Activity A's onPause() method executes.
Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)
Then, if Activity A is no longer visible on screen,
its onStop() method executes.
I just want to know when an activity can start another and still be visible(a concrete example). (because it means that onStop() is always calls when I switch activities)
You can start an Activity B like a dialog if you specify a flag in AndroidManifest.xml:
<activity android:name=".ActivityB"
android:theme="#android:style/Theme.Holo.Light.Dialog"/>
Then Activity B will not take the full screen and you will still see the underlying activity.
onStop() won't be called if you previous activity is visible to user.
onStop will be called only after previous activity is completely invisible to user.
So if new activity is dialog or some transparent background, then previous activity will be displayed to user and onStop() of previous activity won't be called.

in which scenarios onDestroy() is not called?

I know that In Android, you won't get a guaranteed activity's onDestroy() call. My question is in which scenarios onDestroy() is not called?
Suppose there are two activities A and B.Activity A contains a next button and B contains a back button.If you click next,then Activity B will be launched.At that point of time onDestroy() is not called.onStop() of activity A is called after onResume() of B.
Then when you click the back button in Activity B,then onRestart() of A will called after onStop() of B.In this case also onDestroy() will be not called.
When you press the back button of the device onDestroy() will be called at that point of time or if you call finish() while navigating from A to B.
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space.

What is the difference between an activity going in the background and an activity becoming invisible?

I am new to android.I have learned that the onPause() method is called when some other activity comes in the foreground and onStop() method is called when an activity is no longer visible.Can someone please explain what's the difference between going in background and becoming invisible giving some practical example.
Best example? A popup, your activity is still visible, so only onPause() is called. Becoming invisible is when another activity is launched, the app is sent to the background, the user switches to another app et.
Think about two cases
1. Calling Activity B as dialog
In this case, some part of Activty A will be visible. So only onPause() method will get called for Activity A. And when you will be back from Activity B to A, onResume() of Activity A will get called.
2.Calling Activity B as normal activity
In this case, Activity A will be completely invisible, So onPause() and onStop() both will be called. And when you will be back from Activity B to A, onRestart(), onStart() and onResume() methods of Activity A will be called.
Have a look
Case 1: If you call normal activity. (Which is going to hide caller activity)
onPause()
onStop()
<------- Back to caller
onRestart()
onStart()
onResume()
Case 2: If you call activity-as-dialog. (Which is going to partially hide caller activity)
onPause()
<------- Back to caller
onResume()
whenever a dialog box appears on any activity then activity's onPause() called and this activity holds the first position in stack(Top), on the other hand whenever we switches activity from one to another the previous activity's on Stop() method called and its automatically garbage collected in sometimes.

when onStop is actually called

In my simple android app I have 2 activities. In first activity(Main activity) I have override the ON STOP method. But when I go do the second activity on stop method of main_Activity is called. Why this happens? According to my understanding I should call on Pause rather then on Stop.
It calls both. onPause() will be called before onStop(), if onResume() was called. Rougly speaking, onPause() is called when activity is partially hidden, and onStop() when it is fully hidden.
Read up on http://developer.android.com/training/basics/activity-lifecycle/index.html to understand the Activity lifecycle.
onPause() - activity is paused.
onStop() - activity is stopped working but it is there in stack.
onDestroy() - activity is stopped and also has been removed from stack.
therefore When you go from one activity to another activity, it makes onPause() and onStop() but not onDestroy()
OnStop is called when FirstActivity calls SecondActivity and FirstActivity looses
visibility.
If Second Activity has a transparent background then the FirstActivity will be visible underneath, so not loosing visibility and onStop on FirstActivity will never be called.

Activity starting another activity. Guarantee that the first activity's onPause will be called before second's onResume

I have an Activity A and Activity B. Activity A starts Activity B.
Is there a guarantee that A's onPause() will always be called before calling B's onResume()?
This is related to this entry
Found the answer here:
When activity B is launched in front of activity A, this callback will
be invoked on A. B will not be created until A's onPause() returns, so
be sure to not do anything lengthy here.
onPause ()
When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here
http://developer.android.com/reference/android/app/Activity.html#onPause()
Yes. Activity A's onPause() will be called before passing to Activity B's onResume() if the navigation is not the first time. If navigating to Activity B is for the first time, then Activity B's onCreate() will be called after Activity A's onPause(). Take a look at Activity's Lifecycle for clear understanding.

Categories

Resources