0

In answering a question here on SO about when onDestroy is called what appears to be an inconsistency in the Android docs arose.

According to the android docs regarding the task and backstack

"When the user presses the Back button, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored). "

However at the same time, the android lifecycle suggests that activities are not automatically destroyed but rather paused if the UI is partially hidden, stopped if the UI is totally hidden, and destroyed only if the system is low on resources.

These are two opposite positions. So my questions is - which is it?

Shout out to @Raghunandan for going back and forth with me in comments for a while. Hopefully we will get an answer.

Community
  • 1
  • 1
Rarw
  • 7,645
  • 3
  • 28
  • 46

2 Answers2

1

They are both correct in their context. Maybe the lifecycle should say "destroyed only if system is low on resources OR it is popped form the stack".

Consider this for example. You start with activity A, then start activity B from A, then start activity C from B.

  • Now both A and B are paused and stopped, but not destroyed.
  • If C is heavy on resources, A or B may be destroyed.

Now press Back button from Activity C, you are back to Activity B

  • Activity C is paused, stopped and destroyed .

Now press Back button again, you are back to Activity A

  • Activity B is paused, stopped, and destroyed

System tries to keep all the activity instances so they can be re-opened quickly. But when an activity is popped from the stack, there is no way for user to re-open them in future, at least not the same instance.

tjlian666
  • 343
  • 5
  • 17
0

There is no contradiction in these two statements.

onDestroy is called when you press BACK unless you override onBackPressed not to call finish() or do a strange thing of overriding finish and not calling super.finish().

If you don't stop the call to Activity.finish, onDestroy is always called.

The other statement speaks nothing about pressing BACK and I can find nowhere under the link you have provided here that

and destroyed only if the system is low on resources.

Additinally to onDestroy being called when app is low on resources (which strangely doesn't happen on my phone; I get OOM) it is called when 20 other Activities globally (from all applications) are started after this one.

I also encouarge you to check out yet another answer on when onDestroy is called again for a real reason of onDestroy not being called.

Community
  • 1
  • 1
MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • You're probablly right about the typeo in the origninal question i linked to. However, if on destroy was always called automatically everytime back was pressed there would be no need for an `onRestart` method. Even better, if apps progressed only from an active state to a destroyed state why even have the lifecycle methods? If what you're saying is true there are only 2 necesssary methods - onCreate and onDestroy. – Rarw Jun 21 '13 at 12:41
  • @Rarw I suggest [reading more on `Activity` lifecycle](http://developer.android.com/training/basics/activity-lifecycle/starting.html#lifecycle-states). Activity gets paused when the screen goes off or when another transparent `Activity` is put in front of it. Activity gets stopped when you press HOME or when another opaque `Activity` is put on the stack. BACK button is not the only way to have lifecycle methods called. – MaciejGórski Jun 23 '13 at 11:07
  • I'm not suggesting that back is the only way to call a lifecycle method. I am aware they can get called a number of times, for example if you get a phone call or pull down the menu from the top of the screen. But I also don't agree with this idea that onDestroy is called every time back is pressed. Just look at how activities behave when you press the back button. If I back all the way out to home, my app still persists in the background. If all activities were immediately destroyed on press of back, this would not be the case. You would have to restart from onCreate each time. – Rarw Jun 23 '13 at 15:38
  • @Rarw `I also don't agree with this idea that onDestroy is called every time back is pressed`. It's not a matter of agreeing or not. Just put a log statement in `onDestroy`. What makes you think that application process has to be killed once all `Activities` are destroyed? – MaciejGórski Jun 23 '13 at 15:42
  • Because of the way the docs describe it. Let's say I have an app that consists of one activity. That activity loads data downloaded from a server and displays it. If once back is pressed the activity is popped off the stack and destroyed even if the app process persists, I should then have to load onCreate again, redownload and display that information. In my experience this is not what happens. For example, using an app that loads data with a delay between sets such that you can observe the actual loading, on the initial load a delay is observed. Once relaunched from the background it is not – Rarw Jun 23 '13 at 15:46
  • 1
    @Rawr `Talk is cheap. Show me the code. [Linus Torvalds]`. Did you put the log in `onDestroy`? – MaciejGórski Jun 23 '13 at 16:09
  • +1 for the great quote. I put log in onDestroy. You're right It does get called when you press back. But if I press home onDestroy is not called. – Rarw Jun 23 '13 at 16:27