Within my Android app, I utilize Android's ViewPager2 in conjunction with Kotlin and Compose. Initially, everything functioned seamlessly until the integration of Compose. Now, upon returning to the particular fragment containing ViewPager2, I've encountered an issue. Specifically, the createFragment function within the FragmentStateAdapter of ViewPager2 is no longer invoked when isSaveEnabled is set to true. What is the root cause of this problem and would appreciate guidance on resolving it programmatically. How can I address this issue in my code?
binding.viewPager.apply {
this.adapter = adapter
offscreenPageLimit = sections?.size ?: 0
isSaveEnabled = true
}
if (sections?.size!! < 2) {
binding.tabs.visibility = GONE
binding.viewPager.isUserInputEnabled = false
} else
TabLayoutMediator(binding.tabs, binding.viewPager) { tab, position ->
tab.text = sections[position].name
}.attach()
@InternalSerializationApi
class HomeGroupTabAdapter(
fragmentManager: FragmentManager,
lifecycle: Lifecycle,
private val sections: ArrayList<Section>
) : FragmentStateAdapter(fragmentManager, lifecycle) {
override fun getItemCount() = sections.size
override fun createFragment(position: Int) = HomeGroupTabFragment.newInstance(sections, position)
}
@Parcelize
data class Section(
@SerializedName("code")
val code: String,
@SerializedName("items")
val items: List<Item>,
@SerializedName("name")
val name: String
) : Parcelable
@Parcelize
data class Item(
@SerializedName("type")
val type: String,
@SerializedName("code")
val code: String?
) : Parcelable
I tried to set isSaveEnabled to false; however, when returning to the fragment, the createFragment() function is called, but the scroll position is not remembered.