4

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.

Zain
  • 37,492
  • 7
  • 60
  • 84
Acu
  • 41
  • 4
  • 2
    Problem I described concerned androidx.appcompat:appcompat:1.1.0-rc01. Now actual is androidx.appcompat:appcompat:1.1.0 and seems like everything is ok – Acu Sep 10 '19 at 09:49

0 Answers0