Search code examples
androidandroid-fragmentsandroid-viewpager

ViewPager inside a fragment is empty when navigating back from another fragment


I've got a viewPager inside a fragment (A). Inside the viewPager there are 3 another fragments. If I replace the fragment A with fragment B and click back, the viewPager appears with all fragments being empty. Scrolling back and forward reloads the first and third fragments.

I googled a lot, some people suggested to pass the childFragmentManager to the FragmentStatePagerAdapter, but if I do this, the app crashes with " No view found for id ** for fragment **" exception.

I tried to use the FragmentPagerAdapter - same result.

Did anyone come across with this problem and has a suggestion?


Solution

  • Ok, I figured that out. The problem with passing the childFragmentManager was because child fragment doesn't contain the container whose fragment(s) are to be replaced, so the app crashed with "No view found..." exception. However, when replacing the fragment, if I use the parent's fragmentManager, then all works fine! So, instead of fragment.fragmentManager!!.beginTransaction() I now call fragment.parentFragment!!.fragmentManager!!.beginTransaction(). In case someone will face the same problem.