Quantcast
Channel: User HaimS - Stack Overflow
Viewing all articles
Browse latest Browse all 19

Answer by HaimS for Can't change tag of fragment

$
0
0

This is an old question, but anyhow here is some information in the hope that it will help someone.

ViewPager works together with its adapter which is set to it via ViewPager#setAdapter().In the case of this question, it seems FragmentPagerAdapter is used as an adapter (since this adapter names the fragments android:switcher:[x]:[y] as appears in the exception).

One important observation about the FragmentPagerAdapter is this:

FragmentPagerAdapter owns the allocated fragments that are used in the ViewPager.

The user of ViewPager with conjunction of ViewPagerAdapter has to define a subclass of FragmentPagerAdapter implementing the getItem() method, prototyped as follows:

public abstract Fragment getItem(int position);

This function creates the different fragments used by the ViewPager as pages.The implementation of this method should be looked as a service provided by the subclass to FragmentPagerAdapter() for allocating the fragments.

In other words, although the subclass allocates the fragments it is not the owner of them.

Since, as said above, the owner of the fragments is FragmentPagerAdapter, all fragment operations (in the sense of FragmentManager/FragmentTransaction) must only be done by FragmentPagerAdapter.

Trying to perform operations on these fragments may interfere with the normal operation of the ViewPager and cause undefined behavior.

In the case of this question, there is an attempt to remove one of the fragments used by the ViewPager outside of FragmentPagerAdapter which as explained is bad and probably triggered a sequence that ended with this exception.

When it is needed to dynamically remove/add/replace pages in a ViewPager the way to do it to call notifyDataSetChanged() on the adapter.

Good luck!


Viewing all articles
Browse latest Browse all 19

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>