12

I have read that a call to onStop() is always preceded by a call to onPause() no matter what the scenario is. Reference: Figure 1 on http://developer.android.com/training/basics/activity-lifecycle/stopping.html

We know that an activity is paused when it does not have user's focus but it still partially visible, and it is stopped when it is not visible at all.

Now in the scenario when a user is in an activity and from the recent apps window selects and enters another app, the activity will enter from resumed/running to stopped state. No intermediate paused state.

Isn't it?

What is the good reason for always having onPause() before onStop(). We can do in onStop() what is done in onPause(). Why do we always need onPause() before onStop()?

user2882662
  • 535
  • 1
  • 6
  • 18
  • 1
    Have you considered logging in the two methods to see what happens? – Simon Forsberg Nov 05 '13 at 16:00
  • Yes, that is true ... is there any issue you're facing? – gunar Nov 05 '13 at 16:01
  • 1
    The life-cycle diagram will answer your question. – Roy Hinkley Nov 05 '13 at 16:09
  • @SimonAndréForsberg I am beginner to learning android development, I don't know about log and log cat thingy yet. I am learning how to write my first programs. – user2882662 Nov 05 '13 at 16:20
  • @gunar Yes, I have edited my question to say what i mean to ask. i.e. why is an onPause() always needed before onStop()? – user2882662 Nov 05 '13 at 16:22
  • @AndroidAddict I have raised the question from the diagram. I am confused because the scenerio I have mentioned in my question doesn't seem to have a paused state between resumed and stopped state. Moreover, why do we need an onPause() before onStop(), as I have edited my question. – user2882662 Nov 05 '13 at 16:24
  • Now why is my post getting a negative rating! – user2882662 Nov 05 '13 at 16:45

2 Answers2

28

onPause() is always called. This is guaranteed. If you need to save any state in your activity you need to save it in onPause(). onStop() may be called after onPause(), or it may not. Depends on the situation.

There are a lot of lifecycle methods. You don't need to override all of them. You only need to override the ones where you need (or want) to customize the behaviour for your activity. There are a lot of lifecycle methods because different applications have different requirements. The lifecycle of an Activity is well-documented and well-behaved. This allows programmers to put the code exactly where it is needed, based on the particular requirements of the application.

You have asked

What is the good reason for always having onPause() before onStop(). We can do in onStop() what is done in onPause().

onPause() is always called on your Activity if it is in the foreground when Android wants to do something else. It may start another Activity which may result in your Activity's onStop() getting called. It may just call onResume() on your activity. It may just kill your process without calling any more of your lifecycle methods.

Since onStop() is not guaranteed to be called, you can't always do in onStop() what is done in onPause().

In most Activities, you will find that you will need to put code in onResume() and onPause(). You usually don't have to do anything in onStop(), onStart() or onRestart().

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • According to this post, onPause is not called certain conditions. http://stackoverflow.com/questions/9226027/why-onpause-is-not-called-in-following-situation – Boon Sep 13 '16 at 01:21
  • 5
    @Boon and what is the point? `onPause()` is not called if you long press the HOME key to show the "recent tasks", but the `Activity` is not paused, it is still the foremost `Activity` on screen. If the user selects something from the "recent tasks" list, then `onPause()` will be called on your `Activity`, and if the user doesn't select something, then your `Activity` is still the foremost `Activity` on screen. This is pretty much the same as when your `Activity` displays a `Dialog`. In that case, the `Dialog` is covering part of your `Activity`, but your `Activity` is not paused, ... – David Wasser Sep 13 '16 at 19:41
  • 3
    ...`onPause()` is not called on your `Activity`, and your `Activity` is still the foremost `Activity` on screen. In any case, OP asked about the difference between `onPause()` and `onStop()`. If your `Activity` is paused, then `onPause()` is guaranteed to be called. If your app is killed, then `onPause()` is guaranteed to be called on the foremost `Activity`, but `onStop()` may or may not get called. – David Wasser Sep 13 '16 at 19:42
3

Yes, the onPause() method will always be executed. In fact, it is the only method that is guaranteed to be called when your activity is losing focus/stopped/destroyed.

Check out this page: Activity

onResume()
Called when the activity will start interacting with the user again. At this point your activity is at the top of the activity stack, with user input going to it. Always followed by onPause().

Charlie
  • 38
  • 4
luben
  • 2,512
  • 4
  • 30
  • 41
  • 1
    Technically it is as the life-cycle diagram shows. I think what you mean to say is that `onPause` is always called but `onStop` is not. – Roy Hinkley Nov 05 '13 at 16:06
  • "No, the onPause() method will always be executed. " Yes they say so. But why! – user2882662 Nov 05 '13 at 16:25
  • @user2882662 onPause is always called because that is how the ActivityManager allows for the application to clean up so that it does not leak memory. If no method were guaranteed to be called, your app would leak memory every time the ActivityManager killed it. When your app goes into the background, the onPause is executed. `This is where you should be unregistering things like Broadcast Receivers et. al.` onStop may not be called if you app is paused but the ActivityManager kills it and your app will leak memory if it has handles open. – Roy Hinkley Nov 05 '13 at 23:47