1

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

NullPointerException
  • 36,107
  • 79
  • 222
  • 382

5 Answers5

1

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.

Knossos
  • 15,802
  • 10
  • 54
  • 91
  • can you explain me a little more how to it? maybe with code examples? i can not understand you – NullPointerException Nov 23 '10 at 21:48
  • oh, it works, but now i have another problem, when i press the BACK key of the phone.. my app closes! i dont want that, i want that when i press my back key of the phone my app goes to the back intent. How i can solve this? – NullPointerException Nov 23 '10 at 23:01
1

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.

Eliseo
  • 1,547
  • 1
  • 15
  • 14
1

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.

Miguel Morales
  • 1,707
  • 10
  • 10
0

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.

Andrey Novikov
  • 5,563
  • 5
  • 30
  • 51
0

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

animuson
  • 53,861
  • 28
  • 137
  • 147
Android
  • 8,995
  • 9
  • 67
  • 108