Henry Gunawan
Henry Gunawan

Reputation: 942

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...

Upvotes: 0

Views: 91

Answers (3)

NPovlsen
NPovlsen

Reputation: 335

This should work:

activity.finish();
Intent intent = new Intent(activity, cls);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT);
}
activity.startActivity(intent);

Upvotes: 0

Sandeep Malik
Sandeep Malik

Reputation: 1976

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);
    }

Upvotes: 1

niceumang
niceumang

Reputation: 1432

Try ! Below code definitely work for you. ;)

Intent intent = new Intent(QuestionaireFinalActivity.this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Upvotes: 0

Related Questions