1

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??

5
  • 3
    Android is designed around the idea that the system, rather than the user or the developer, decides when to dispose of an application's process. Situations where it is truly necessary to over-ride that are, while not unheard of, somewhat rare. Why is it essential that you do this? Aug 30, 2011 at 7:19
  • You should let Android decides when is the perfect moment to destroy your app. If you see the "life cycle of Android's apps" you will understand that the only you must to do to finish your app is make a .finish(); in your main activity. The management of the apps its perfect like it is.
    – Aracem
    Aug 30, 2011 at 7:35
  • 1
    @Chris - I think you're too categoric. Simple example when application for sure need to be closed - it's smth like password manager
    – Barmaley
    Aug 30, 2011 at 8:13
  • @barmaley - actually, that's an example of the kind of situation where relying on terminating the application process is a bad idea, because it's contrary to the android design intent and so may not continue to work. Instead, you should negate some aspect of the session/login information to terminate it irrespective of the death or survival of the process. That way you have an application that behaves as needed when android behaves (perhaps at some point more forcefully) as it was designed to. Aug 30, 2011 at 14:20
  • try this stackoverflow.com/questions/3226495/…
    – LMK
    Jun 1, 2013 at 10:22

2 Answers 2

2

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);
1
  • using this method, is there pass data one activity to second through intent ? Aug 30, 2011 at 7:24
1

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."

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.