I am basically trying to call a fragment from another fragment with a button.
I used supportFragmentManager
for that.But when I try to use it it gives me
Unresolved reference: supportFragmentManager
error.Even I add requireActivity().supportFragmentManager
it 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