Android - addToBackStack() doesn't work?

前端 未结 3 1346
旧时难觅i
旧时难觅i 2021-02-07 14:08

This is the function responsible for adding fragments to back stack:

public void populateContent(File f)
{

    ContentFragment cf = new ContentFragment(ctx, ac,         


        
3条回答
  •  北海茫月
    2021-02-07 14:33

    I was adding, not replacing, but I had to call addToBackStack(null) before add(), not after, on the fragment which will close, not on the fragment which will stay open.

    In class A (opened first)

    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.layout_a, f, Constants.FRAGMENT_KEY);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
    

    In class B (opened by class A)

    FragmentTransaction ft = fm.beginTransaction();
    ft.addToBackStack(null);
    ft.add(R.id.layout_b, f, Constants.FRAGMENT_KEY);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
    

提交回复
热议问题