Android - addToBackStack() doesn't work?

前端 未结 3 1350
旧时难觅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: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() 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.

提交回复
热议问题