0

I have one confusion that when system call onPause and onStop method of activity lifecycyle. I see that by navigating to another app from my activity, here system is calling onPause and onStop and when I start new another activity of my application again system is calling onPause and onStop method

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startActivity(new Intent(this, SecondActivity.class));
}

Here this answer says "onPause() call when one Activity transfer control to another Activity, and onStop() method call when Activity in finish mode." that seems wrong to me that when I am starting another activity both are calling and I simply start the activity and did not finish it but still onPause and onStop method is calling.

Doc says "When your activity is not longer then it call onStop" Here I am OK it calls onStop

Confusion when your activity is no longer visible and hidden by another activity but still onPause is also calling but why ??

Community
  • 1
  • 1
N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • read this http://developer.android.com/reference/android/app/Activity.html – Raghunandan Apr 08 '14 at 06:23
  • @Raghunandan I have read that could not understand perfectly – N Sharma Apr 08 '14 at 06:24
  • Check here also: [Activity Lifecycle](http://developer.android.com/training/basics/activity-lifecycle/starting.html). Simply said: `onPause()` is called when the activity is hidden partially (e.g. `AlertDialog` showing). `onStop()` is called when the activity is hidden completely (e.g. transferring to another activity). And I kind of agree that the linked answer is not completely correct. – Andrew T. Apr 08 '14 at 06:27
  • @Williams the docs has topid visible lifetime and foreground lifetime. that should be enough to understand the lifecycle of the activity. You could also log info overriding activity lifecycle methods to check for yourself instead of looking at other posts – Raghunandan Apr 08 '14 at 06:28
  • @Raghunandan If I have already understood then otherwise I would have not asked here – N Sharma Apr 08 '14 at 06:29
  • @Williams did you try logging info in life cycle methods to check yourself?? – Raghunandan Apr 08 '14 at 06:31
  • @Raghunandan Yes That why I shared my research in my question – N Sharma Apr 08 '14 at 06:47
  • @Raghunandan Confusion when your activity is completely hidden by another activity but still `onPause` is calling but why ?? – N Sharma Apr 08 '14 at 06:56
  • after onPause its onStop – Raghunandan Apr 08 '14 at 06:58
  • Yes I know but why `onPause` is calling docs says activity is no longer visible, it only call `onStop` – N Sharma Apr 08 '14 at 06:58

1 Answers1

1

To keep it simple, here is the small info from the android developers documentation.

Activity Lifecycle

Activities in the system are managed as an activity stack. When a new activity is
started, it is placed on the top of the stack and becomes the running activity -- 
the previous activity always remains below it in the stack, and will not come to the
foreground again until the new activity exits.

An activity has essentially four states:

If an activity in the foreground of the screen (at the top of the stack), it is active or running.
If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

Please refer to this life cycle demo application. It's really helpful in understanding the Activity lifecycle.

Also as Raghunandan suggested, you must read this.

Added:

For instance: When a user traverses from Activity A to Activity B (FullScreen Non Transparent) following things happens

  1. Activity A's state changes to paused state (which calls onPause on Activity A)
  2. Then Activity A's state changes from paused to stopped state (which calls onStop on Activity A) . onStop is called because Activity B was full screen non transparent activity. (If Activity B is non-full-sized or transparent then onStop is NOT called on Activity A)

onPause is guaranteed to be called on Activity A (No matter whether Activity B is non-full-sized transparent or full-sized). onStop will only be called when Activity A is completely overridden by full-sized Activity B.

So in onPause you can save an Activity's state or some other useful info if required.

Good Luck :)

Vishal Vyas
  • 2,571
  • 1
  • 22
  • 39
  • If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations. ---- Here how it is possible when a new full sized activity has focus on your activity then that your previous activity still visible ?? – N Sharma Apr 08 '14 at 06:53
  • @Williams If the full size Activity B has focus and is on Top of Activity A then Activity A cannot be visible and is in stopped state. Activity B will be visible and running. Please refer to this http://developer.android.com/training/basics/activity-lifecycle/index.html demo project from Android team. It'll really help you to understand the life cycle callbacks. Hope I answered your question. – Vishal Vyas Apr 08 '14 at 07:07
  • I have already an example. Confusion when your activity is no longer visible and hidden by another activity but still onPause is calling but why ?? – N Sharma Apr 08 '14 at 07:08
  • @Williams This is the way how Activity life cycle works. onPause is guaranteed to be called when one Activity will be overridden by some other Activity (No matter whether it si non-full-sized transparent or full-sized). onStop will only be called when an Activity is overridden by some other full-sized Activity. So in onPause you can save an Activity's state or some other useful info if required. – Vishal Vyas Apr 08 '14 at 07:19
  • @Williams you can also check this post http://stackoverflow.com/questions/9266417/difference-between-onpause-and-onstop – Vishal Vyas Apr 08 '14 at 07:22
  • But you said above in the answer `onPause` is calling when If an activity has lost focus but is still visible , how any activity visible when you start full sized normal activity (Non-Transparent) – N Sharma Apr 08 '14 at 07:39
  • @Williams I already said in comments. If the full size Activity B has focus and is on Top of Activity A then Activity A CANNOT BE VISIBLE and is in STOPPED state. Activity B will be VISIBLE and RUNNING. – Vishal Vyas Apr 08 '14 at 07:46
  • I KNOW THIS CONCEPT BUT HERE B ACTIVITY IS NOT TRANSAPRENT BUT STILL `onPause` METHOD IS CALLING THAT WAS MY QUESTION, YOU ARE SAYING THAT "you said above in the answer onPause is calling when If an activity has lost focus but is still visible , " – N Sharma Apr 08 '14 at 07:47
  • onPause method will be called on Activity A and then onStop will be called on Activity A. For Activity B, onCreate,onStart and onResume will be called. – Vishal Vyas Apr 08 '14 at 07:50
  • Yes Right but your words "onPause is calling when If an activity has lost focus but **is still visible**" -- here Activity A is not visible and completely hidden by Activity B but here `onPause` of Activity A is calling that is opposite as you are saying – N Sharma Apr 08 '14 at 07:52
  • For instance: When a user traverses from Activity A to Activity B following things happens 1) Activity A's state changes to paused state (which calls onPause) 2) Then Activity A's state changes from paused to stopped state (which calls onStop) as Activity B was full screen non transparent activity. This is the way life cycle works – Vishal Vyas Apr 08 '14 at 07:57