Getting reference to nested fragment from FragmentTabHost

后端 未结 4 514
走了就别回头了
走了就别回头了 2021-02-07 05:11

In my application, I use an Activity which holds one Fragment with FragmentTabHost and hence all its tabs are nested Fragments

4条回答
  •  遇见更好的自我
    2021-02-07 05:50

    I found a solution that I like a little better because it doesn't involving executing code with a delay (which is always iffy given android hardware fragmentation and different processor speeds).

    In your onTabChanged() method, before you try to find the fragment, call executePendingTransactions() on the fragment manager associated with your tabHost. It seems there are some places in the FragmentTabHost source code where they should be calling executePendingTransactions() but fail to do so.

    This works every time the tab changes with one exception... the first tab that is selected still comes back null... In my specific case, I was able to handle this exception differently anyway, by putting some code in onResume.

    Hope this helps.

提交回复
热议问题