0

I'm trying to close an app with a button with this method:System.exit(0); But I get back to the other activity ! How can I fix that ? Thanks

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Catfoxes
  • 101
  • 1
  • 9
  • Take a look at this. http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238#2034238 – Varun Sep 10 '13 at 20:21
  • Why would you even want to do this? Do you have a service to stop? You should not close an Android app without a good reason. – Simon Sep 10 '13 at 20:30

2 Answers2

2

You should not use System.exit(0). Use finish() instead .finish will pop the activity from the activity back stack and destroy it. Previous activity in the back stack takes focus.

Check this link and comments by Dianne Hackborn

https://groups.google.com/forum/#!topic/android-developers/Zhd1fRfHAAQ

Check this link and comments by Romain Guy

https://groups.google.com/forum/#!topic/android-developers/G_D3pKnGLt0

Quoting from comment by Streets of Boston

https://groups.google.com/forum/#!topic/android-developers/Y96KnN_6RqM

You should not call System.exit(). It could mess up Android's handling of the lifecycles of your activities and result in an awkward user-experience (e.g. when killing the process, the previous activity from which you laucnhed your activity may be gone as well. Android may try to restart the process again and re-create that accidentally killed parent-activity. but still).

public static void exit (int code)

Added in API level 1
Causes the VM to stop running and the program to exit. If runFinalizersOnExit(boolean) has been previously invoked with a true argument, then all objects will be properly garbage-collected and finalized first.

Parameters
code    the return code.

Is quitting an application frowned upon?

If you are looking for navigation use actionbar and navigation bar.

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
1

It's not recommended, but this should completely kill off the app.

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

To exit normally you should just invoke Finish() from your main activity.

Jon
  • 1,398
  • 9
  • 14
  • Thanks but finish() just close one activity – Catfoxes Sep 10 '13 at 20:15
  • 1
    @user2670035 you should use actionbar/navigationbar on click on home icon navigate to app home screen and click back button to exit the app. but you should not use `System.exit(0)`. http://developer.android.com/design/patterns/navigation.html – Raghunandan Sep 10 '13 at 20:19
  • You can pass or retrieve the context for your main activity and call it with that. – Jon Sep 10 '13 at 20:23