简体   繁体   中英

Fragment disappears with ViewPager2 and FragmentStateAdapter

I'm building an app using ViewPager2 that displays different fragments when swiped. My adapter extends FragmentStateAdapter as described here .

When I put my app into the background and then resume again, the current fragment disappears. What do I need to do to make sure the current fragment is restored correctly?

(Note: I am asking about FragmentStateAdapter , not FragmentPagerAdapter or FragmentStatePagerAdapter .)

Update 1 :

My fragment class is as follows:

class PagerFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val layoutResId: Int = arguments!!.getInt(ARG_LAYOUT_RES_ID)
    return inflater.inflate(layoutResId, container, false)
}

companion object {
    private const val ARG_LAYOUT_RES_ID = "layoutResId"

    fun newInstance(layoutResId: Int): PagerFragment {
        val args = Bundle()
        args.putInt(ARG_LAYOUT_RES_ID, layoutResId)

        val fragment = PagerFragment()
        fragment.arguments = args
        return fragment
    }
}

}

I pass in the desired layout resource id for each fragment when it is created.

My view pager adapter class is as follows:

class ViewPagerAdapter(fm: FragmentManager) : FragmentStateAdapter(fm) 
{
    override fun getItem(position: Int): Fragment {
        return PagerFragment.newInstance(Helper.getLayoutResId(position))
    }

    override fun getItemCount(): Int {
        return Helper.getSize()
    }
}

Adding retainInstance = true does not resolve the problem.

Update 2 :

Calling notifyDataSetChanged() in onResume() in my main activity solves the problem for now, although it feels like a heavier hammer than should be necessary. (The original ViewPager + FragmentStatePagerAdapter does not require this type of call to reload fragments properly after bringing in the app from the background.)

Update 3 :

Updating my dependency from androidx.viewpager2:viewpager2:1.0.0-alpha01 to the recently released androidx.viewpager2:viewpager2:1.0.0-alpha02 fixes the issue entirely :).

Instead of FragmentManager you need to pass your FragmentActivity or Fragment instance as FragmentStateAdapter constructor parameter.

private inner class ViewPagerAdapter(fragment: Fragment) :
    FragmentStateAdapter(fragment) {

     val fragments = arrayOf(
        MyFragment1(),
        MyFragment2()
    )

    override fun createFragment(position: Int): Fragment {
        return fragments[position]
    }

    override fun getItemCount(): Int {
        return fragments.size
    }
}

Official Documentation: Read Here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM