7

I'm calling finish() if a certain condition was met. However, even after finish() if i hold the home button i can still see my app there. If killing the process is not a desirable option, what should I do?

This is where i'm calling finish():

    protected void onStart() {
.............
if(today.equals("..."))
finish();

    };

I know that finish will finish the activity but why is it still visible in the task manager? I've finished it.

ARMAGEDDON
  • 939
  • 3
  • 11
  • 23
  • `finish()` just kills the Activity, not the whole application. What are you trying to do? Why do you need it to be gone? – dmon Jun 06 '13 at 16:29
  • but my app has only one Activity. Main.java only. – ARMAGEDDON Jun 06 '13 at 16:30
  • Yeah, but there's a process that contains any activities you have, even if you have only one, and that's the one that is still running. – dmon Jun 06 '13 at 16:30
  • This [question](http://stackoverflow.com/questions/6330200/how-to-quit-android-application-programmatically) may help – Gustek Jun 06 '13 at 16:31
  • the thing is that when pressing and holding the home button to open task manager the user can see the app with the progress bar, and it's like not really professional showing the progress bar within the app in the application task manager – ARMAGEDDON Jun 06 '13 at 16:34

5 Answers5

10

finishAndRemoveTask() is the new API that as per the documentation "Finishes all activities in this task and removes it from the recent tasks list."

if(android.os.Build.VERSION.SDK_INT >= 21)
{
    finishAndRemoveTask();
}
else
{
    finish();
}
Varun Bhatia
  • 4,326
  • 32
  • 46
9

Pressing (and holding) HOME button doesn't open task manager. This is some sort of "Recent Apps" section which shows you recently opened apps. Actually it is a bit smarter than that - it can restore last visited activity, but since you have only one activity, this shouldn't be really important for you

So basically even if you can see your app in this list, it doesn't mean it is actually running.

Pavel Dudka
  • 20,754
  • 7
  • 70
  • 83
  • The only correct and exact answer is the one with less votes (until now at least)! You've got my up vote! – thiagolr Jun 06 '13 at 17:52
  • After looking up different ways to exit an application `onDestroy()` or `finish()` or `android.os.Process.killProcess(android.os.Process.myPid()); super.onDestroy();` in `onDestroy()`... They all led to this behavior. I am glad to see that the activity is not on the stack and just hiding away in a list of recent apps. Thank you! +1 – tricknology Sep 04 '14 at 23:02
  • 1
    While it is true that the recent's isn't exactly a task manager, this misses the main point made by Srikant, which is that finish() is not by design intended to normally result in process termination. – Chris Stratton May 26 '15 at 22:09
5

finish() tells the android system that you are done. It is up to the android system to decide when to actually kill your application. It will keep your application around as long as the OS doesn't require the resources. For the OS, keeping your application in RAM is cheaper than restarting the app. This strategy is true for most modern OS.

Srikant Sahay
  • 885
  • 6
  • 9
  • Also as @Pavel Dudka mentioned, long pressing the home button doesn't open the task manager, but simply opens list of recently launched apps (at least on the phones I have seen). – Srikant Sahay Jun 06 '13 at 16:39
5

If killing the process is not a desirable option, what should I do?

Nothing, most likely.

I know that finish will finish the activity but why is it still visible in the task manager?

First, your process is still running. This is an optimization, so if the user elects to return to your app right away, or something else triggers your app code (e.g., AlarmManager), Android does not have to waste CPU and battery forking another process for you right away. Android will terminate your process when it deems this to be appropriate.

Note that depending on what you consider the "task manager" to be, your app may appear there anyway, even if your process is terminated. For example, long-pressing HOME or pressing the RECENTS button brings up "recent tasks", which includes tasks for which the process has been terminated.

it's like not really professional showing the progress bar within the app in the application task manager

I have no idea why you would think that. That being said, you are welcome to override onCreateThumbnail() in your activities to return something else to use for the thumbnail image.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

Processes stay in Suspended state :

 PID USER       VSZ STAT COMMAND
 --- ----       --- ---- -------
 5846 root         0 SW<  [ksdioirqd/mmc1]
 5853 u0_a76    306m S    {android.youtube} com.google.android.youtube
 5897 root      1852 S    /sbin/sh -
 5922 u0_a79    286m S    {om.me.mygdxgame} com.me.mygdxgame
 5947 u0_a49    278m S    {roid.dspmanager} com.bel.android.dspmanager
 5965 root      1844 R    ps -w

Even when all Activities are closed.

There is no harm in keeping stuff loaded in RAM when there's plenty space. Volatile RAM IC's consumes power, empty or full. Suspended processes do not hog CPU. These processes will be terminated when space is required. Until then, they serve to be quickly resumable and make OS faster.

The situations when you'll need to look for running processes are:

  1. Poorly managed run away threads. You start a thread which runs a loop, don't know it will ever end, and on top loose all references to it.
  2. Badly implemented, and very often, leaked Services. Registering service with a system manager/service and forgetting to unregister.
  3. System processes run via Runtime.exec(), and forgetting to call destroy() on them.

If you are just dealing with simple Android components and have implemented their life-cycle correctly. You never need worry about internal processes.

S.D.
  • 29,290
  • 3
  • 79
  • 130