简体   繁体   中英

How to remove activity from activity backstack?

I have two activities Activity A and Activity B. When I click button in Activity A, The Activity B starts. Now When I press back button from Activity B the Activity A get restarted. But I want to come out of the app when the back button in Activity B is pressed. I tried using this but not getting success

Intent intent=new Intent(ActivityA.this, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Thanks in advance

You need to finish ActivityA once you're starting ActivityB :

Intent intent=new Intent(ActivityA.this, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

Call finish() after starting new Activity - this will close the calling Activity:

startActivity(new Intent(ActivityA.this, ActivityB.class));

//calling finish() closes current Activity
finish();

Read more about Activity life cycle here and here .

Just call finish() after startActivity(intent).

Intent intent=new Intent(ActivityA.this, ActivityB.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
finish();

Intent.FLAG_ACTIVITY_CLEAR_TOP is not suitable for your situation.In your code,ActivityA will re-builded again.There has two instances of ActivityA in the TaskStack.Delete the addFlag.

Intent intent=new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
finish();

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