title: Change Fragment in Navigation Drawer Activity (Kotlin)
body: <p>I've just started using <strong>Fragments</strong> and <strong>ViewModels</strong>. I want to replace fragments inside my activity, how can I do that?</p>
  
  <p>What is the best approach to mix Android and Fragments together?</p>
  
  <p><strong>MainActivity</strong> </p>
  
  <pre><code>class MainActivity : AppCompatActivity(),NavigationView.OnNavigationItemSelectedListener {
  
      lateinit var drawerLayout: DrawerLayout
      lateinit var fragmentTransaction: FragmentTransaction
  
      private lateinit var appBarConfiguration: AppBarConfiguration
  
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          val toolbar: Toolbar = findViewById(R.id.toolbar)
          setSupportActionBar(toolbar)
  
          drawerLayout= findViewById(R.id.drawer_layout)
          val navView: NavigationView = findViewById(R.id.nav_view)
          val navController = findNavController(R.id.nav_host_fragment)
  
          val toggle = ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_open)
              drawerLayout.addDrawerListener(toggle)
              toggle.syncState()
  
  
          // Passing each menu ID as a set of Ids because each
          // menu should be considered as top level destinations.
          appBarConfiguration = AppBarConfiguration(setOf(
                  R.id.nav_home, R.id.nav_dersler, R.id.nav_soru,R.id.nav_socialmedia), drawerLayout)
          setupActionBarWithNavController(navController, appBarConfiguration)
          navView.setupWithNavController(navController)
  
          navView.setNavigationItemSelectedListener(this);
  
      }
  
      override fun onNavigationItemSelected(item: MenuItem): Boolean {
          when(item.itemId){
              R.id.nav_home -&gt; {
                    //Toast.makeText(this,"Anasayfa",Toast.LENGTH_SHORT).show()
                  }
                  R.id.nav_socialmedia -&gt; {
                      Toast.makeText(this,"Sosyal Medya Hesaplarımızı Takip Edebilirsiniz",Toast.LENGTH_SHORT).show()
                  }
  
                  R.id.nav_dersler -&gt; {
  
  
                  }
                  R.id.nav_soru -&gt; {
  
                  }
  
  
              }
              drawerLayout.closeDrawer(GravityCompat.START)
              return true
  
          }
  
  
          override fun onCreateOptionsMenu(menu: Menu): Boolean {
              menuInflater.inflate(R.menu.main, menu)
              return true
          }
  
  
  
  
          override fun onOptionsItemSelected(item: MenuItem): Boolean {
               when(item.itemId){
                  R.id.action_refresh -&gt; {
                      Toast.makeText(this,"Refreshing...",Toast.LENGTH_SHORT).show()
                      val refreshActivity = intent
                      finish()
                      startActivity(refreshActivity)
                      true
                  }
  
                  else -&gt; super.onOptionsItemSelected(item)
              }
              return true
          }
  
          override fun onSupportNavigateUp(): Boolean {
              val navController = findNavController(R.id.nav_host_fragment)
              return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
  
          }
  
  
  }
  </code></pre>
  
  <p>And i have mobile_navigation.xml but i don't know what it does. Thank you for your help 
  <a href="https://i.stack.imgur.com/Pa0hK.png" rel="nofollow noreferrer">enter image description here</a></p>