简体   繁体   中英

navigation between 2 fragments

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.

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();
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM