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

Muhammad Yaseen :

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

Zeeshan Majeed :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=26183&siteId=1