NullPointerException
NullPointerException

Reputation: 37733

How i can close/exit button on my app?

i have a button to close my app with this code:

finish();

the problem is that this button doesn't exit of my app... it simply closes the current intent ant returns to the previous intent (window) of my app....

how i can do a real exit/close button?

i tryed with this:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

but it doesn't works, because when i turn back into my app, the app comes in the last opened window, and not in the first window of the app, how i can do that? i need that when i re-open my app it starts on the first window of my app

Upvotes: 1

Views: 1520

Answers (5)

Android
Android

Reputation: 9033

you can try this: System.exit(0);

Upvotes: 0

Miguel Morales
Miguel Morales

Reputation: 1707

I recommend you read this: http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html

Chances are, you don't want an exit button. Perhaps a logout button, but that's it.

Upvotes: 1

Eliseo
Eliseo

Reputation: 1557

I would do it this way:

  • I would define my initial activity (i.e. MainMenu) with a Launch Mode of singleTop
  • I would then invoke my MainMenu from the activity that is going to close the application.

    startActivity(new Intent(this, MainMenu.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("closeProgram", true);

  • Then override the onNewIntent in the MainMenu activity; check for the extra boolean of "closeProgram", if the value is true, then do a finish();

Haven't tried it but I think it should work.

Upvotes: 1

Knossos
Knossos

Reputation: 16068

If you really want your app to die. You could initiate each intent with startActivityForResult(). then before each finish() set the result to send back. in each parent activity you can override onActivityResult() to test whether the result received means the application needs to end. if so you can call another set result and finish(). repeat this in all activities and you will find that your application terminates entirely.

Incidentally I'm writing this from memory. function names may not be exact.

Hope that helps.

p.s. re-read your requirements. you can always stop the finish loop at your first activity.

Upvotes: 1

Andrey Novikov
Andrey Novikov

Reputation: 5593

finish() closes activity (this is what you call intent, but it's not correct), not application. Application can not be finished at all (only forcefully killed like task killers do). You should design your activity stack in such a way that it will suit your needs. May be you should look at stack rearrangement examples in ApiDemos.

Upvotes: 0

Related Questions