switch from one fragment to another in main activity

Example of what i'm trying to achieve https://i.stack.imgur.com/lhp10.png

I'm stuck on the part where i need to switch to createUserFragment from defaultFragment

i attached this to my button, but nothing happens when i press it, not sure what's wrong there

MainActivity

class MainActivity : AppCompatActivity() {

    lateinit var appBarConfiguration: AppBarConfiguration
    lateinit var navController: NavController


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        
        navController = findNavController(R.id.hostFragment)


        appBarConfiguration = AppBarConfiguration(navController.graph,drawer_layout)

        NavigationUI.setupActionBarWithNavController(this, navController,drawer_layout)

        NavigationUI.setupWithNavController(navigation_drawer,navController)

    }


}

override fun onSupportNavigateUp(): Boolean {
        return NavigationUI.navigateUp(navController,appBarConfiguration)
    }

defaultFragment

class defaultFragment : Fragment() {
    private lateinit var binding: defaultFragmentBinding

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

        // Inflate the layout for this fragment
        binding = defaultFragmentBinding.inflate(inflater)
        return (binding.root)
    }

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

       

        binding.createNewBlankFAB.setOnClickListener{
            val transaction = (activity as MainActivity).supportFragmentManager.beginTransaction()
            transaction.replace(R.id.defaultFragment, createUserFragment())
            transaction.disallowAddToBackStack()
            transaction.commit()
        }
    }

}

My questions are: 1)How can i fix my thing? 2)Will it work properly? I.e no memory leaks or some other funky stuff 3)Do i have to use another fragment for data input or maybe there's another way?

UPDATE I stil have no idea how this works, but apparently i was replacing wrong fragment, i switched this R.id.defaultFragment in transaction.replace to R.id.hostFragment from which, i assume, all other fragments spawn, but now it just spawn on top of my existing fragment and the drawer button doesn't change its state, i guess i have to either do all of this differently or somehow pass to the drawer navigation information that current fragment was changed?

2 answers

  • answered 2020-09-24 16:02 Gintoki

    I think you have to call "transaction.add" instead of "replace" since you are calling your fragment from an activity. Replace is called when you call a fragment from another fragment, I believe.

  • answered 2020-09-24 16:52 Wahdat Kashmiri

    This is how we navigate within fragments in Navigation component library . We use navigate to then id of the destination Fragment which is defined in navgraph

    binding.createNewBlankFAB.setOnClickListener{
            
        Navigation.findNavController(view).navigate(R.id.createUserFragment);
            }