android - Can't change tag of fragment -


i have few pages (fragments) in viewpager.
when tried remove (for instance) first one, this:

mfragmentmanager.begintransaction().remove(page1).commit(); 

it says that

illegalstateexception has occured. 

i don't know why. me? thanks.

exception:

java.lang.illegalstateexception: can't change tag of fragment page2{40586418 #2 id=0x7f080006 android:switcher:2131230726:2}: android:switcher:2131230726:2 android:switcher:2131230726:1 

this old question, anyhow here information in hope someone.

viewpager works adapter set via viewpager#setadapter(). in case of question, seems fragmentpageradapter used adapter (since adapter names fragments android:switcher:[x]:[y] appears in exception).

one important observation fragmentpageradapter this:

fragmentpageradapter owns allocated fragments used in viewpager.

the user of viewpager conjunction of viewpageradapter has define subclass of fragmentpageradapter implementing getitem() method, prototyped follows:

public abstract fragment getitem(int position); 

this function creates different fragments used viewpager pages. implementation of method should looked service provided subclass fragmentpageradapter() allocating fragments.

in other words, although subclass allocates fragments not owner of them.

since, said above, owner of fragments fragmentpageradapter, fragment operations (in sense of fragmentmanager/fragmenttransaction) must done fragmentpageradapter.

trying perform operations on these fragments may interfere normal operation of viewpager , cause undefined behavior.

in case of question, there attempt remove 1 of fragments used viewpager outside of fragmentpageradapter explained bad , triggered sequence ended exception.

when needed dynamically remove/add/replace pages in viewpager way call notifydatasetchanged() on adapter.

good luck!


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -