Viewpager
with FragmentStatePagerAdapter
in androidx
. When I try to replace a fragment on selected position (say pos 0) I get java.lang.IllegalArgumentException: Cannot setMaxLifecycle for Fragment not attached to FragmentManager
FragmentStatePagerAdapter
is constructed in following way:
private inner class Adapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {...}
Before migrating to androidx
it was constructed as:
private inner class Adapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {...}
and everything worked fine.
With androidx
this way works OK as well but this constructor is now deprecated in androidx
.
I replace fragment using a method in adapter which triggers notifyDataSetChanged()
then method
override fun getItemPosition(`object`: Any): Int { .. }
on some conditions returns POSITION_NONE and subsequently
override fun getItem(position: Int): Fragment { ... }
creates and returns new fragment to replace the old one. However I got the above mentioned exception.
I do not show code because implementation of above methods in adapter seems to be irrelevant.