1

AdapterClass:

class TeamsAdapter(
supportFragmentManager: FragmentManager,
teams_list: MutableList<Team>,
match_id: String
) : FragmentPagerAdapter(supportFragmentManager) {code}

Fragment Class:

lateinit var teamsAdapter: TeamsAdapter

teamsAdapter = TeamsAdapter(supportFragmentManager, match?.teams!!, matchId)

Error: Unresolved reference: supportFragmentManager

But the entire code works fine when inside an Activity instead of Fragment Class.

4 Answers 4

7

Solved by casting activity if you are using fragment then cast with context

  fragmentManager = (activity as FragmentActivity).supportFragmentManager
Sign up to request clarification or add additional context in comments.

1 Comment

Using the Activity's fragment manager for a ViewPager hosted within a Fragment is always the wrong thing to do. You should use the childFragmentManager for nesting fragments.
1

you can use parentFragmentManager or childFragmentManager for using fragment manager inside of a fragment. There is no need to use any sort of casting for using a fragment manager.

parentFragmentManager or getParentFragmentManager()

Return the FragmentManager for interacting with fragments associated with this fragment's activity. Note that this will available slightly before {@link #getActivity()}, during the time from when the fragment is placed in a {@link FragmentTransaction} until it is committed and attached to its activity.If this Fragment is a child of another Fragment, the FragmentManager returned here will be the parent's {@link #getChildFragmentManager()}. @throws IllegalStateException if not associated with a transaction or host.

childFragmentManager or getChildFragmentManager()

Return a private FragmentManager for placing and managing Fragments inside of this Fragment.

I just read that you are learning something new, so I suggest it is better to use the best practices. Casting sometimes generates errors and crashes, so I suggest you use these.

1 Comment

That seems ok as far as IDE allows however when compiling all I receive is "Unresolved reference: parentFragmentManager". I went back to using activity.supportFragmentManager in my fragments.
1

When your fragment contains a ViewPager that uses fragments, you must always use the childFragmentManager for that ViewPager as that's what allows FragmentManager to properly nest those fragments and ensure that their state is properly saved and restored.

Comments

-1

You should use supportFragmentManager if you are supporting device below API 14, otherwise just use fragmentManager

1 Comment

Current implementation of androidx.fragment.app.Fragment is based on support library (android.app.Fragment was deprecated). So, you cannot use fragmentManager, only supportFragmentManager . Also fragmentManager was deprecated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.