Reputation: 703
i have 2 fragments A and B in my application. the mainactivity starts up with fragment A. on pressing a button in it i replace it with fragment B.
FragmentManager fm = getSupportFragmentManager();
B_Fragment pfrag = new B_Fragment();
pfrag.setArguments(args);
fm.beginTransaction().replace(R.id.frag_container, pfrag)
.addToBackStack("A_Fragment").commit();
Now in fragment B i press a button to replace it with fragment A using:
fm.popBackStack();
fm.beginTransaction().addToBackStack("B_fragment").commit();
Fragment A is successfully pushed and popped from the stack where as fragment B is not. Every time B_fragment is been destroyed and a new one is created. So can someone tell me what i am missing and how to push fragment B onto the stack and pop A out at the same time.
Upvotes: 0
Views: 1893
Reputation: 23622
FragmentManager fm = getSupportFragmentManager();
B_Fragment pfrag = new B_Fragment();
pfrag.setArguments(args);
fm.beginTransaction().replace(R.id.frag_container, pfrag).commit();
On Fragment B
, why don't just replace it with Fragment A
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.pnlLeft, details);
ft.commit();
popBackStack
does not load the last fragment, it's commonly used to pop the entire stack :
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
before loading another fragment
beginTransaction()
replace() Or add()
commit()
When you press the button in Fragment B
, try the below code. This will re-load the entire fragment
.
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack();
}
Upvotes: 2