Catfoxes
Catfoxes

Reputation: 69

Closing completely an app not just an activity

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

Upvotes: 0

Views: 479

Answers (2)

Jon
Jon

Reputation: 1388

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.

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

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.

Upvotes: 2

Related Questions