简体   繁体   中英

Activity Do not clear from back stack even From Method Intent.FLAG_ACTIVITY_CLEAR_TOP

I want to move to another activity but before i want to clear all the other activity. I know the method Intent.FLAG_ACTIVITY_CLEAR_TOP but it does not work when i back pressed after moving then it again show the previous one activity. My code is

 Intent intent = new Intent(LoginActivity.this, WelcomeActivity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 startActivity(intent);
 progressDialog.dismiss();

Thanks Advance

CLEAR_TOP only work if you have single Top means only one activity So you also need to add Intent. Clear Task and Intent. New Task with your code that look like this in the below code

   intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
    Intent.FLAG_ACTIVITY_CLEAR_TASK |
    Intent.FLAG_ACTIVITY_NEW_TASK);

Sometimes it doesn't work you should add this line in your code

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK | 
        Intent.FLAG_ACTIVITY_CLEAR_TASK);

It will help you and hopefully your issue will resolve. I've added this line in your code and after adding that line your code will look like this

 Intent intent = new Intent(LoginActivity.this, WelcomeActivity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
    Intent.FLAG_ACTIVITY_CLEAR_TASK |
    Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);
 progressDialog.dismiss();

Replace your code with the above code.

Add three flags:

 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM