Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear the stack activity on Back button pressed

Tags:

java

android

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.

like image 266
Goofy Avatar asked Sep 23 '26 04:09

Goofy


1 Answers

Intent intent= new Intent(this, screen 1.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
like image 109
Kanaiya Katarmal Avatar answered Sep 24 '26 19:09

Kanaiya Katarmal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!