For a android tablet application I use 2 fragments. One on the left side on the screen and one on the right side. When you click on a button at the right fragment, another f
You could simply make your own back stack and override onBackPressed()
and handle it in any fashion you see fit. I don't think there is any other way to achieve the desired behavior.
Your own back stack could simply be an ArrayList
of tags for your fragments.
This solution only works for your case. You needn't put Fragment2 into backstack.What you can do is removing the code 'ft.addToBackStack("Fragment2");'. The Fragment2 is not in the stack.So you close the Fragment2, and leave [fragment 3] on the screen.
You can create your own fragment manager/observer. It will be just a simple list or map inside. Track them using tag or simply class name (getClass). This way you'll be able to avoid new fragment creation and reuse cached one, thus controlling your memory consumption. You will also be able to manipulate fragments the way you want (override onBackPressed and push which ever fragment you like), instead of using only the way Google says it should/can be done.
I just answered a similar question, so here's a link to that answer.
The implementation allows you to remove a Fragment
from anywhere in the backstack and this is achieved by using refrection to modify the internal variables that store the backstack information in FragmentManagerImpl
and BackStackRecord
.
Although I do not encourage the use of it, I hope it helps.