Search code examples
androidandroid-activity

Remove or destroy all activity beside current one


I have an app with this sequence of activities:

Login > Activity 1 > Activity 2 > Activity 3 > HomeActivity

In Activity 3, there is a button for opening HomeActivity. After that, in HomeActivty I press back button and it can still go back to Activity 3 when it shouldn't. I tried these but still doesn't work:

Intent intent = new Intent(QuestionaireFinalActivity.this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Intent intent = new Intent(QuestionaireFinalActivity.this, HomeActivity.class);
startActivity(intent);
finish();

Are there any other ways to destroy all activity before HomeActivity so when back button is pressed, it will close the app? Please help...


Solution

  • use this method to clear all activities:

    public static void clearAllIntent(Intent intent){
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
                    Intent.FLAG_ACTIVITY_CLEAR_TASK |
                    Intent.FLAG_ACTIVITY_NEW_TASK);
        }