0

Possible Duplicate:
Quitting an application - is that frowned upon?

I have an Android app on Android. The way it starts is that it has a main menu, then you click start, and that is where the main function is. I was wondering, once I am in an activity that is not on the bottom of the stack, I want to allow the user to exit the program. Is there a way to finish the entire application, not just the current activity?

Community
  • 1
  • 1
Ephraim
  • 8,352
  • 9
  • 31
  • 48
  • you can find the answer here http://stackoverflow.com/questions/5478200/android-yes-i-want-to-close-the-app-i-opened – papachan Mar 30 '11 at 15:10

3 Answers3

0

Though this is not generally considered acceptable, you would do it like this:

System.exit(0)

Like I said though, this is not how Android apps work usually.

Edit: I guess I should be more clear in saying that it isn't generally acceptable. Google doesn't want apps forcing a close, as it can have unforeseen consequences. The way you should close your app is by manually finishing every activity.

Chris Cashwell
  • 22,308
  • 13
  • 63
  • 94
  • Google requests that this be reserved for test cases at the most and should not be used in production code. – CommonsWare Mar 30 '11 at 15:09
  • @CommonsWare: That's exactly why I said that it's not generally considered acceptable behavior for an app. – Chris Cashwell Mar 30 '11 at 15:10
  • 1
    You will note that I did not say you were wrong. However, IMHO, "not generally considered acceptable" is a bit weaker than "Google says don't do that", which is why I wanted to clarify the point. – CommonsWare Mar 30 '11 at 15:11
0

Please refer to Mark's answer.

There are also some useful Intent flags, in particular FLAG_ACTIVITY_CLEAR_TOP, which will finish activities until the intent target is reached. This is used often for apps that begin with a login screen and need to go back to it when the user logs out.

Community
  • 1
  • 1
Matthew
  • 44,826
  • 10
  • 98
  • 87
0

Calling the System.exit(0) command can have unforeseen consequences in your application lifecycle according to this discussion, and as mentioned in other answers and comments is recommended against by Google for production code.

The suggestion on this site seems reasonable:

Call finish() on your activities. If you have multiple activities to close, use startActivityForResult() and pass back a "close now" flag so you know to call finish() on the parent activity as well.

Brian
  • 16,196
  • 3
  • 27
  • 28