Android - addToBackStack() doesn't work?

前端 未结 3 1347
旧时难觅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:21

    It seems that calling addToBackStack() on fragment transaction is not enough, we have to handle the popping up of the back stack upon Back button pressed by ourselves. I added this to my activity and it worked as expected:

    @Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() > 0 ){
            getFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
    }
    
    0 讨论(0)
  • 2021-02-07 14:24

    I am not sure about the actual solution but I can guide you a bit and perhaps you can figure out what the problem is.

    Are you actually replacing two fragments? If you do not then there is no transaction to revert. Also is your first Fragment added from XML? The manager will not know about this fragment and you might need to add the first Fragment using a transaction too.

    Be careful to check for if (savedInstanceState == null) performFirstTransaction() otherwise you will end up adding your first Fragment twice.

    One good idea is to use enableDebugLogging in the FragmentManager. This will tell you about which fragments the manager knows about.

    See this: http://developer.android.com/reference/android/app/FragmentManager.html#enableDebugLogging(boolean)

    As a side note, it is NOT recommended to use a custom constructor for your Fragment. That is because if your app gets killed and re-instantiated by the OS it will call the empty constructor.

    You should use a static method such as ContentFragment.getInstance(<params>) to create your Fragment.

    See more info at: http://developer.android.com/reference/android/app/Fragment.html at the "Class Overview" section.

    I hope my answer helps you a little bit to find the problem.

    0 讨论(0)
  • 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();
    
    0 讨论(0)
提交回复
热议问题