How i can close/exit button on my app?

前端 未结 6 1678
慢半拍i
慢半拍i 2021-01-23 20:45

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 retu

相关标签:
6条回答
  • 2021-01-23 21:26

    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.

    0 讨论(0)
  • 2021-01-23 21:31

    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.

    0 讨论(0)
  • 2021-01-23 21:32

    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.

    0 讨论(0)
  • 2021-01-23 21:35

    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:

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    
    0 讨论(0)
  • 2021-01-23 21:38

    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.

    0 讨论(0)
  • 2021-01-23 21:46

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

    0 讨论(0)
提交回复
热议问题