Android Reorder Fragment Backstack

后端 未结 2 1464
难免孤独
难免孤独 2020-12-03 03:29

I have a number of pages/fragments listed in my navigation drawer, the user is likely to switch between these frequently and I want them in the backstack so that they can na

2条回答
  •  广开言路
    2020-12-03 03:57

    There is no API to do this, but becuse I was wondering the same thing last week,
    I took it as an exercise an implemented it myself.

    My method allows you to remove a Fragment from anywhere in the backstack and this is achieved by using refrection to modify the internal variables that store the backstack information in FragmentManagerImpl and BackStackRecord.

    There are few important variables:

    • mBackStack - stores the BackStackRecords that hold the Fragment info, the previous ones, animations, etc
    • mActive - all added Fragments
    • mAvailBackStackIndices - stores the indices that can be used by the newly inserted records, i.e. indices of null values in mActive
    • and other

    Tested with 150+ Fragments and couldn't notice any leaks, but only monitored the heap in DDMS and did not perform any other memory analysis.
    So because someting may be broken, explore the code, re-test it, see if anyone provides a better answer, and rethink if you really need to do this in your project.

    I uploaded the code as a gist on GitHub, hope it helps.

提交回复
热议问题