17

So I have my Activity, and on pressing a "Quit" button I call Activity.finish(). This effectively closes my application.

The problem: The Dalvik-process of my application is still hanging around as a zombie in background. It seems like this is normal as other applications do the same. Even The hello-world example hangs around in memory..

I could live with this, but unfortunatley this behaviour makes the development of my application a pain. I have a remote service connected to my Activity, and this service won't unload until my Activity unloads (which as said it never does).

Everything is somehow kept alive for no good reason.

How can I really remove my Activity from memory?

I'm looking for something like Activity.finish_and_kill_my_process_please() call or something similar.

8 Answers 8

26
  1. Call finish(); on button click
  2. Add this line to onDestroy method:

android.os.Process.killProcess(android.os.Process.myPid());

    public void onDestroy() {
        super.onDestroy();
        android.os.Process.killProcess(android.os.Process.myPid());
    }       
3
  • 8
    For a released app KillProcess() isn't a good idea. But for the development phase (speeding up termination of an app for debugging purposes) this is a life saver. +1.
    – an00b
    May 25, 2011 at 0:45
  • Definitely Useful while developing(for temporary use)
    – Diljeet
    Jul 21, 2012 at 16:23
  • 1
    This has been useful to help me to unload a native lib :-) Sep 7, 2012 at 14:17
7

So I have my Activity, and on pressing a "Quit" button I call Activity.finish(). This effectively closes my application.

Please don't do this.

The Dalvik-process of my application is still hanging around as a zombie in background.

The process is kept in a cache, for potential reuse by Android. You can safely ignore it.

I have a remote service connected to my Activity, and this service won't unload until my Activity unloads (which as said it never does).

You probably have a bug in your application, such as calling startService() and failing to call stopService(), or calling bindService() and failing to call unbindService().

6

Try using the killBackgroundProcesses method:

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("name.of.your.package");

You will need to add the KILL_BACKGROUND_PROCESSES permission to your AndroidManifest.xml file.

3
  • Ok, well it appears this method is no longer part of the ActivityManager class??
    – bugzy
    Feb 3, 2011 at 22:17
  • There is a new API for Android 2.2 Froyo but this is a VERY bad idea. That permissions is marked as DANGEROUS and users will see that your app has the ability to kill other apps.
    – Tom
    Jul 30, 2012 at 20:53
  • While terminating an app's process is almost never a good idea outside of obscure tests during development, this is an unnecessarily roundabout way to do it. A process can simply exit() or kill itself - it doesn't need to ask the ActivityManager to do so. May 21, 2015 at 13:06
5

By killing the process you can stop the application completely.from the last screen on back button press you can kill the process by :

public void onBackPressed() {   
        super.onBackPressed();   
        int pid = android.os.Process.myPid();
        android.os.Process.killProcess(pid);    }
3

Well, if not only activity, but also the application you want to terminate, you can use

System.exit(0)

People say that it's deprecated or bad example, but, well it's pure Java and does work...

4
  • Do you know why people say those things? Does it have something to do with how the application "lives" within the Android O.S.? This is tempting to use in one of my apps.. but I'd like to know the theory behind the discouragement first. Apr 5, 2012 at 2:35
  • 2
    Well, I think they say it because Android can manage applications itself; so we shouldn't poke our nose in. I say this is bullsh*t though: If it's my application, who else but me can know when to terminate it? :) May 18, 2012 at 13:27
  • System.exit is not equivalent to killing a process and may resume any activities on the activity stack that have been paused. See here for more: proandroiddev.com/…
    – SullX
    Aug 8, 2020 at 5:29
  • Thanks @SullX! I guess this is a good example on why it's not recommended :) It seems Android can restart the application if there are 'unfinished' activities left after System.exit() call... Didn't know about this at all! :) Sep 28, 2020 at 4:31
0

By using the ActivityManager we can close the another applications also.

private ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
am.restartPackage("com.jimmy.appToBeClosed");

to use the above code should have the android.permission.RESTART_PACKAGE permissions in manifest file.

1
  • restartPackage is now deprecated. use killBackgroundProcesses instead.
    – rfinz
    Jul 5, 2012 at 18:58
0

Similar to the System.exit(0) on the Java side, you can also call a native method during onDestroy which calls exit(0) on the C/C++ side. This seems to force the lib to exit completely. It's working for me!

-1

System.exit() instantly unloads whole virtual machine with all native libraries, services and etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.