Skip to content Skip to sidebar Skip to footer

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

Solution 1:

By default Android's design doesn't favour exiting an application, but rather allows the OS to manage it. You can bring up the Home application by it's corresponding Intent:

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

Solution 2:

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.

Solution 3:

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.

Solution 4:

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.

Solution 5:

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.

Post a Comment for "How I Can Close/exit Button On My App?"