Fragment Lifecycle Callbacks Not Triggering on ViewPager2 with Nested Fragments in Android 14
I'm trying to implement I'm converting an old project and Does anyone know how to I'm writing unit tests and I can't seem to get I've been working on this all day and I'm experiencing an issue where the lifecycle callbacks in my nested fragments are not triggering as expected when using ViewPager2 in an Android 14 application... I have a main fragment that contains a ViewPager2, which in turn hosts several child fragments. The child fragments are supposed to implement `onStart()` and `onResume()`, but I'm finding that these methods are not being called when I swipe between pages. Hereβs a simplified version of my implementation: ```kotlin class MainFragment : Fragment() { private lateinit var viewPager: ViewPager2 private lateinit var adapter: ViewPagerAdapter override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val view = inflater.inflate(R.layout.fragment_main, container, false) viewPager = view.findViewById(R.id.viewPager) adapter = ViewPagerAdapter(this) viewPager.adapter = adapter return view } } class ViewPagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) { override fun getItemCount(): Int = 3 override fun createFragment(position: Int): Fragment { return ChildFragment.newInstance(position) } } class ChildFragment : Fragment() { companion object { fun newInstance(position: Int) = ChildFragment().apply { arguments = Bundle().apply { putInt("position", position) } } } override fun onStart() { super.onStart() Log.d("ChildFragment", "onStart called for position: ${arguments?.getInt("position")}") } override fun onResume() { super.onResume() Log.d("ChildFragment", "onResume called for position: ${arguments?.getInt("position")}") } } ``` I have confirmed that the `ViewPager2` is correctly initialized and the child fragments are being created. However, the logs for `onStart` and `onResume` in `ChildFragment` do not appear when I swipe between pages. I've tried using `ViewPager2.setOffscreenPageLimit(1)` and ensuring that the child fragments are not being destroyed when they go off-screen, but the lifecycle methods still aren't being called. Is there a known issue with ViewPager2 and fragment lifecycle events, or am I missing something fundamental in my implementation? Any insights or suggestions would be greatly appreciated. For context: I'm using Kotlin on macOS. Any ideas what could be causing this? How would you solve this? This is happening in both development and production on Ubuntu 22.04. Is this even possible? I'm working with Kotlin in a Docker container on Ubuntu 20.04. Any suggestions would be helpful. Thanks, I really appreciate it! I'm coming from a different tech stack and learning Kotlin. The project is a CLI tool built with Kotlin. Could this be a known issue?