Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove an activity from stack in android

I want to remove an activity from stack using code.Heres my case

  1. From page A I am going to page B.
  2. From page B i have to return to page A using return button.
  3. In page B I am having a button which takes to page C.
  4. When I click that button in page B , I am calling
finish(); //to remove PageB from stack

Ok, Here is the issue, From Page C when I click the return button I am taken to page A. because it is in stack.

I want to remove Page A from stack when I click the button in page B.

Please note that I cant call a finish() in page A when calling Page B because I want to return back to page A. Only case I dont want to return is when the button in page B is clicked.

How can I do this in android? Thanks

like image 579
Zach Avatar asked Jan 18 '23 17:01

Zach


1 Answers

Rather than calling startActivity in A when you start B, call startActivityForResult. Then in, your activity for A, handle onActivityResult.

Now, in B, when you open C, call setResult before calling finish. This will allow you to set some data to get passed back to A's onActivityResult method. Pass a flag to indicate that A should close itself and then call finish. Handle that flag in A's onActivityResult.

This way, each activity is responsible for closing itself and you're not artificially messing with the back stack. Using intent flags work fine in a simple A,B,C case, but will probably fall apart if these 3 screens are part of a larger solution (i.e. A,B and C are deep under a stack of activities you don't want to mess with).

like image 114
Ben Von Handorf Avatar answered Jan 30 '23 10:01

Ben Von Handorf