Been googling several sites on how to navigate/traverse between fragments in Android. It is not easy because it is mostly in Java and they are so old and deprecated. Finally after piecing together the information from several sites, I managed to get it working.
I better document it down for my own use in future. Don’t want to go through the tiring process of googling again.
At MainActivity.kt , add the function “replaceFragment”.
val fragmentManager = supportFragmentManager
val transaction = fragmentManager.beginTransaction()
The id of my activity_main.xml constraint layout is “host”. By default it is empty when you create a new project.
To generate onViewCreated. Right click “Generate…” > “Override Methods…”. Then search for “onViewCreated”.
val context = activity as AppCompatActivity
In fragment_one.xml , setOnClickListener for button1 so that it will navigate to fragment_two.xml