0

I want to destroy an Activity and call the finish() method to do so. In versions below Android 5.0.2 the Activity actually gets destroyed, but in Android versions above it wanders to the background instead of ending. But onDestroy() still gets called. Is there a flag I have to set or something for an activity to actually be destroyed no matter what an OS might want to say about that?

Floern
  • 33,559
  • 24
  • 104
  • 119
Nerethar
  • 327
  • 2
  • 16
  • 1
    "in Android Versions above it wanders to the background instead of ending" -- and your proof of this is, what, exactly? "to actually be destroyed" -- by your own admission, the activity *is* being destroyed ("But onDestroy() still gets called"). Please explain **completely and precisely** what you are seeing that you are not expecting. – CommonsWare Sep 25 '15 at 11:33
  • I didn't know that I need a proof. But my most notable observation is that the activity I want to be destroyed can be selected in the list of open tasks on devices. And that is not what I expect after calling finish() on an activity, when it is not a single activity application. One of my reasons to not expect that behavior is that on devices below Android 5.0.2 the said behaviour does not apply. – Nerethar Sep 25 '15 at 11:51
  • 2
    the *list of open tasks* is not actually list of **open** tasks, but the list of **recent** tasks. You can do almost nothing about that. – Vladyslav Matviienko Sep 25 '15 at 11:53
  • you are talking about the recent apps button, those are **recently used** apps and not necessarily apps in background. – Rahul Tiwari Sep 25 '15 at 11:57
  • Oh, I did not know that, that is pretty confusing. This actually leads to a situation where to activities of the same app are shown in said list. How can I make sure that only one activity is shown in this list at any given time? – Nerethar Sep 25 '15 at 11:58

1 Answers1

1

But my most notable observation is that the activity I want to be destroyed can be selected in the list of open tasks on devices

Sure. It's been that way for years. The recent-tasks lists (now referred to as the overview screen) shows recently-used tasks. This is not new to Android versions higher than 5.0.2. For example, the following screenshot shows the recent-tasks list from a Galaxy Nexus running Android 4.1.2:

Recent-Tasks List

I started up this device from a cold boot, launched the contacts ("People") app from the home screen, pressed BACK to exit and destroy that activity, then brought up the recent-tasks list and took the screenshot.

And that is not what I expect after calling finish() on an activity, when it is not a single activity application. One of my reasons to not expect that behavior is that on devices below Android 5.0.2 the said behaviour does not apply.

Then you have fallen through a rupture in the space-time continuum and wandered into a parallel universe, one where your unexpected behavior is how Android has been working for years and what ~1 billion people are used to. Also, in this parallel universe, I don't have hair, much to my chagrin.

There are ways to block your activity from showing up in the recent-tasks list (e.g., android:excludeFromRecents). However, that is not typical behavior.

How can I make sure that only one activity is shown in this list at any given time?

The list is of tasks, not activities. The only reason that your activity would appear multiple times in the list is if your activities are top-most on more than one task's BACK stack. You can take steps to try to manage this (e.g., android:taskAffinity) if the defaults are not working for you.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Well, I know that apps do not end when you press finish, but in an app with multiple activities finish usually killed an activity and did not include it in said list till you finish the last activity. At least this is the behaviour I tested with something about ten devices. But your android:ExcludeFromRecents guided me to the right solution. I used finishAndRemoveTask, that seems to be an addition to Lollipop – Nerethar Sep 25 '15 at 12:28