Transaction between fragments only inside one ActionBar Tab

后端 未结 1 871
遥遥无期
遥遥无期 2021-02-02 03:44

I have an app with three tabs (ActionBar Tabs), each one with one fragment at a time.

TabListener

TabsActivity

Tab1 -> ListFragment1 -> ListFragment2 ->

相关标签:
1条回答
  • 2021-02-02 04:19

    You're not missing anything (or I'm missing it too).

    I searched long and hard for a way to do this "properly" but I couldn't find anything. What I ended up doing is writing my own backstack logic. Unfortunately my employer owns my code so I can't share any of that verbatim, but here was my approach:

    Create an enum with one entry for each of your tabs. Let's call it TabType. Now create an instance variable tabStacks of type HashMap<TabType, Stack<String>>. Now you can instantiate one stack for each tab - each stack is a list of tags, as specified by Fragment.getTag(). This way you don't have to worry about storing references to Fragments and whether they're going to disappear on you when you rotate the device. Any time you need a reference to a Fragment, grab the right tag off the stack and use FragmentManager.findFragmentByTag().

    Now whenever you want to push a Fragment onto a tab, generate a new tag (I used UUID.randomUUID().toString()) and use it in your call to FragmentTransaction.add(). Then push the tag on top of the stack for the currently displayed tab.

    Be careful: when you want to push a new fragment on top of an old one, don't remove() the old one, since the FragmentManager will consider it gone and it will be cleaned up. Be sure to detach() it, and then attach() it later. Only use remove() when you're permanently popping a Fragment, and only use add() the first time you want to show it.

    Then, you'll have to add some relatively simple logic to your TabListener. When a tab is unselected, simply peek() at its stack and detatch() the associated Fragment. When a tab is selected, peek() at the top of that stack and attach() that fragment.

    Lastly, you'll have to deal with Activity lifecycle quirks (like orientation changes). Persist your Map of Stacks as well as the currently selected tab, and unpack it again in your onCreate(). (You don't get this packing and unpacking for free, but it's pretty easy to do.) Luckily your TabType enum is Serializable so it should be trivial to put into a Bundle.

    0 讨论(0)
提交回复
热议问题