Foobar
Foobar

Reputation: 8487

Fragments Not Displaying After Switching To Different Fragment And Back

I have an activity with three different fragments that are switched to via a bottom navigation view. The middle fragment, TutorialFragment, has a viewpager that switches between two other fragments.

The problem is that if I switch from TutorialFragment to a different fragment and then back to TutorialFragment via the bottom navigation view, the fragments inside TutorialFragment's viewpager won't display.

Here's an example (notice how after I switch to "feedback" from "tutorial" and then back to "tutorial", the "Use Keyboard Fragment" and "Enable Keyboard Fragment" strings in the top right of the app no longer show):

Screenshot

Here is the code for TutorialFragment:

class TutorialFragment : Fragment() {

    private lateinit var tutorialView : View

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        tutorialView = inflater.inflate(R.layout.fragment_tutorial, container, false )
        tutorialView.tutorialViewPager.adapter = TutorialFragmentPagerAdapter(fragmentManager!!)
        tutorialView.circleIndicator.setViewPager(tutorialView.tutorialViewPager)
        return tutorialView
    }

Upvotes: 0

Views: 187

Answers (1)

Foobar
Foobar

Reputation: 8487

The solution turned out to be replacing

tutorialView.tutorialViewPager.adapter = TutorialFragmentPagerAdapter(fragmentManager!!)

with

tutorialView.tutorialViewPager.adapter = TutorialFragmentPagerAdapter(childFragmentManager)

Upvotes: 0

Related Questions