Prachi
Prachi

Reputation: 1004

How to close/exit an application in android?

I tried closing the application using the finish(), but finish() method just finishes the activity, the application does not closes. Now I am using System.exit(0) to close the application sometimes it works but sometime it throws an exception. How can I completely exit from an application??

Upvotes: 1

Views: 1625

Answers (2)

azernik
azernik

Reputation: 1228

Android's memory model is a bit weird - basically, the OS will decide for you when it wants to actually exit. As long as your app is not using bandwidth or excessive CPU time (read: the user's money or CPU time), the Android system's design is such that you're supposed to just leave it running, and the system will kill the app itself when it needs the resources.

What you need to do is stop using (through finish() or other means) bandwidth or CPU time or the like, reset any state you need in order to provide your desired user experience, drop any wake locks or similar miscellaneous hardware modifications you've made, and end any services you really don't want to keep running with stopSelf().

If/when the OS runs out of memory (which is the only time when it really matters whether an activity is actually gone or just not doing anything at the moment) it will unceremoniously force kill activities or services (without running any cleanup code or event handlers) based on a fairly complex set of priorities that basically boils down to "don't throw out anything that the user is using/will notice."

Upvotes: 1

Okay, you can do onething get ProcessID of your application and in onDestroy() kill that process. Thats it

int pid=android.os.Process.myPid();
android.os.Process.killProcess(pid);

Upvotes: 2

Related Questions