Close all running activities in an android application?

后端 未结 11 1749
死守一世寂寞
死守一世寂寞 2020-12-11 19:19

I create one application and never use finish() for each activity. If my user clicks on the logout button it goes to the previous page.

How can I close my previous

相关标签:
11条回答
  • 2020-12-11 19:47

    you can add a List in Application that save every activity you created. and when you exit, you just need to finish all activity in the list.

    0 讨论(0)
  • 2020-12-11 19:50

    you can use this
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

    from this question answer How to exit from the application and show the home screen?

    0 讨论(0)
  • 2020-12-11 19:53

    put this line of code in your application System.exit(2);

    0 讨论(0)
  • 2020-12-11 19:54
    Intent intent = new Intent(this, LoginActivity.class);
    ComponentName cn = intent.getComponent();
    Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
    startActivity(mainIntent);
    

    Description:

    public static Intent makeRestartActivityTask (ComponentName mainActivity)
    

    Make an Intent that can be used to re-launch an application's task in its base state. This is like makeMainActivity(ComponentName), but also sets the flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK.

    0 讨论(0)
  • 2020-12-11 19:55

    use finish() for each activity

    1) Activity lifecycle

    2) Shutting down Activity and managing Activities lifecycle

    3) The same question answered in detail (2 approaches - Broadcast Receiver (+1) and Intent.FLAG_ACTIVITY_CLEAR_TOP)

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