Snippet does not update content after returning from previous stack

I have a ZooFragment that contains a ViewPager . This ViewPager has three children: LionFragment , LeopardFragment and TigerFragment , each of these children can request a transaction to call a new ZooFragment .

When a ZooFragment called zooA (with arguments) is initialized, all three children in the ViewPager display the contents. From any child fragment, the user call will invoke a new ZooFragment called zooB (with different arguments, of course).

Based on transaction action from child fragment to ZooFragment :

1.If I use transaction.replace() , zooB will be empty, all three children in zooB will not display the contents, empty. In zooB pressing the Back key from the navigation, zooA becomes empty.

2.If I use transaction.add() , zooB will not be empty, then after clicking the Back button, zooA will be empty.

In the ZooFragment class ZooFragment I load data into onCreateView() , so why do all child fragments in the ViewPager become empty?

+4
source share
1 answer

Please do not replace fragments in ViewPager, just show (using a transaction) in any other contained in the same layout as in ViewPager, and this container should be FrameLayout. The device also allows you to find out when and which fragment appears. Then request fresh data from a utility that responds to updating your fragment. I do not think that onDestroyed will be called in case of all fragments in the ViewPager. The fragments in the ViewPager are fixed, so if the contents of the application do not disappear until the application manager wants to destroy it. Instead of trying to replace fragments in the adapter, try changing another set of fragments and notifyDataSet or take advantage of FrameLayout to show another fragment over the current fragment of the presentation pager tab.

There is my solution that works:

Scroll at the fragment level used with ViewPager with the default swipe disabled

0
source

Source: https://habr.com/ru/post/1500453/


All Articles