0

I am basically trying to call a fragment from another fragment with a button. I used supportFragmentManagerfor that.But when I try to use it it gives me

Unresolved reference: supportFragmentManager

error.Even I add requireActivity().supportFragmentManagerit doesn't gives me any error about supportFragmentManager but this time there is a error about replace method.It gives me

Type mismatch: inferred type is FilterFragment but Int was expected

error.I want to go from that layout to other layout.So it must be this but it requires int when I convert my supportFragmentManager to requireActivity().supportFragmentManager .I don't know how to solve that.

Here is my FilterFragment Class to go ListFragment Class with button click

class FilterFragment : Fragment(R.layout.fragment_filter) {
    private lateinit var fragment_list:ListFragment
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        fragment_list= ListFragment()
        
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        btn_back.setOnClickListener {
            loadFragment(fragment_list)
        }

    }
    private fun loadFragment(fragment: Fragment){
        val transaction = requireActivity().supportFragmentManager.beginTransaction()
        transaction.replace(this, fragment)
        transaction.disallowAddToBackStack()
        transaction.commit()
    }

}

And My ListFragmentClass

class ListFragment : Fragment(R.layout.fragment_list) {

    object KEY {
        const val KEY_NAVIGATOR = "NAVIGATOR"
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val bundle = this.arguments
        if (bundle != null) {
            val navigator : ListNavigator = bundle.getSerializable(KEY_NAVIGATOR) as ListNavigator
            navigator.navigateFilter()
        }
    }

}

Edit:

After adding the code below I fixed that error.

private fun loadFragment(fragment: Fragment){

       /* parentFragmentManager.beginTransaction().apply {
            replace(fragmentContainerView.id,fragment,ListFragment::class.java.simpleName)
                .addToBackStack(null).commit()

        }*/
        val transaction = getChildFragmentManager().beginTransaction()
        transaction.add(fragmentContainerView.id, fragment)
        transaction.disallowAddToBackStack()
        transaction.commit()
        Log.d("JUPITER","I AM HERE")
    }

And also added android:id="@+id/fragmentContainerView" code to my fragment_filter.xml

But still can't load to my fragment well.It looks like below. When I click the button it is trying to load the other fragment in same page with first fragment. The Situation which I face now

Before I click the button

Ömer
  • 13
  • 5

1 Answers1

0

You have to use childFragmentManager or parentfragmentmanager instead of supportfragmentmanager depending on your usecase since you're trying to call a fragment from another fragment.

eg

Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.video_fragment, videoFragment).commit();
Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31
  • I can't reach my fragment with that way I can onyl acces it with R.layout.fragment_filter.With R.id.fragment isn't exist for me.And method still wants id.And I can't use it. – Ömer Jul 28 '22 at 08:55
  • https://stackoverflow.com/questions/6847460/fragments-within-fragments See if this helps – Narendra_Nath Jul 28 '22 at 09:05
  • your code actually worked but It is removing old fragment to left and other fragment(which I try to load) is loading on the right and don't dipslay.The circular thing is spinning.I mean old fragment still on the screen and don't remove meanwhile new fragment is trying to be loaded but never loads. – Ömer Jul 28 '22 at 11:04
  • Use transaction.replace(R.id.video_fragment, videoFragment).commit(); Also do accept thte answer if it helped you – Narendra_Nath Jul 28 '22 at 11:14
  • Still same unfortunately.You can see how it looks like right now I edited on the question's below – Ömer Jul 28 '22 at 11:17