How to open a Fragment
from another Fragment
?
I basically have a button inside of my HomeFragment.kt
and I want to make it to redirect me to AboutFragment
but I'm not sure how to make it. I tried different methods from Stack but nothing worked... Any suggestions? I wanna open the new fragment as I will open from navigation drawer.
Here is the button but my code just work with activities... Not with fragments...
binding.aboutUsBtn.setOnClickListener {
val intent = Intent(activity, AboutFragment::class.java)
startActivity(intent)
}
CodePudding user response:
Try this code plz:
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.home_layout, new ChatFragment(), "second fragment"); //My second Fragment
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();