简体   繁体   中英

Clear the stack activity on Back button pressed

I have 4 activities:

Menu -> screen 1
Menu items -> screen 2
Detailed View -> screen 3
Ordered item -> screen 4

In Menu activity(screen 1) I have a button onclick of that it goes to MenuItems activity(scrren 2 which is List view), on click of any item in the list view it to corresponding item detailed view in this activity(screen 3) I have a button called ordered view, onlclick of this it would go to Ordered item(screen 4), in this scrren 4 I have a button which will go to screen 2.

This is the flow

Screen 1 ->screen 2->screen 3->screen 4->screen 2

Prob: after doing the basic flow now when I click on back in screen 2 it goes to screen 4 and again clicking on back it goes to screen 3 which is resulting the user to click back button n no of times since the activity already exists in stack.

How to handle this I mean back button navigation.

I have tried using flags in intent but it's not working for me.

I have also referred this

  1. Android: Remove a series of Activites on clicking back

  2. On logout, clear Activity history stack, preventing "back" button from opening logged-in-only Activites

edit I got the answer: so as few of my friends have answered here I used this Intent.FLAG_ACTIVITY_CLEAR_TOP

So

screen 1->screen 2->screen 3-> screen 4->screen 2

So in screen 4 I have to set this flag so that it will clear all the activities above that activity.

Intent intent= new Intent(this, screen 1.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

you have to use finish() after every intent so this activity will be close.

The you can intent function from 4 activity to 2 activity . Also use finish after it.

What you want to do is set screen 2 to have launchMode singleTop in the manifest. This ensures that there is only one of those activities in your stack at one time. You also want to set the clear top flag in your intent.

Intent a = new Intent(this,A.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(a);

you have to use finish() after every intent so this activity will be close.

The you can intent function from 4 activity to 2 activity . Also use finish after it.

http://nanostuffs.com/Blog/?p=607

u have to check the link in this link uses a child activity for a main activity for tab .

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