Reputation: 86363
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.
Upvotes: 17
Views: 41745
Reputation: 629
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); }
Upvotes: 5
Reputation: 2388
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!
Upvotes: 0
Reputation: 436
System.exit() instantly unloads whole virtual machine with all native libraries, services and etc.
Upvotes: -1
Reputation: 942
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...
Upvotes: 3
Reputation: 691
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.
Upvotes: 0
Reputation: 359
android.os.Process.killProcess(android.os.Process.myPid());
public void onDestroy() {
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());
}
Upvotes: 26
Reputation: 1006964
So I have my Activity, and on pressing a "Quit" button I call Activity.finish(). This effectively closes my application.
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()
.
Upvotes: 7
Reputation: 200120
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.
Upvotes: 6