How to switch from one fragment to another in Android Application using Kotlin programmatically?

How to switch from one fragment to another in Android Application using Kotlin programmatically?

                                
                                  

// Get the instance of a fragment you want to switch to val fragment : Fragment = MainFragment.newInstance() // Get instance of Fragment Manager Transaction val transaction = activity?.supportFragmentManager!!.beginTransaction() // Replace fragment with current fragment you wish to switch to transaction.replace(R.id.frame_container, fragment) // Set transaction stack to null transaction.addToBackStack(null) // Commit and finalize the transaction transaction.commit()

Programming Language: Kotlin