Я получаю исключение в Viewpager Adapter (FragmentStatePagerAdapter)
, говоря
java.lang.IllegalStateException: Fragment already added: CodeAnswerFragment{8c1a4d1 #0 id=0x7f0902fb}
Я использовал basefragment
для настройки всех Fragments
.Это работает для некоторого фрагмента, но не для всех.хотя код такой же во фрагментах
inner class AnswerFragmentAdapter(private val answerFragmentList : List<BAF>,
fm : FragmentManager) : FragmentStatePagerAdapter(fm) {
override fun getCount(): Int {
return answerFragmentList.size
}
override fun getItem(position: Int): Fragment {
return answerFragmentList[position]
}
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
// Ignored : Avoid reloading answer fragment
}
}
Вот где я настраиваю ViewPager
val answers : List<Answer> = state.loadingExtras as List<Answer>
if(answers.isNotEmpty()) {
val answerFragmentList: ArrayList<BAF> = arrayListOf()
var bundle : Bundle
var answerFragment: BAF
for (answer in answers) {
bundle = Bundle()
answerFragment = getNewAnswerFragmentInstance()
bundle.putSerializable(BaseAnswerFragment.KEY_ANSWER, answer)
answerFragment.arguments = bundle
answerFragmentList.add(answerFragment)
}
setUpViewPager(answerFragmentList)
protected open fun setUpViewPager(answerFragmentList : List<BAF>) {
activity?.supportFragmentManager?.let {
answerFragmentAdapter = AnswerFragmentAdapter(answerFragmentList, it)
}
baseAnswerSectionDataBinding.layoutViewpager.viewpagerAnswer.adapter =
answerFragmentAdapter
}