I want to remove an activity from stack using code.Heres my case
- From page A I am going to page B.
- From page B i have to return to page A using return button.
- In page B I am having a button which takes to page C.
- 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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With