title: Android Navigation Drawer with fragments that open other sub fragments
body: <p>I'm developing an Android app (I'm a newbie) that uses a Navigation Drawer. I've created multiple fragments that represent the various item in the side menu. One of them has a RecyclerView, and I want to show another fragment with various details when the user clicks one of the item of the RecyclerView.</p>
  
  <p>I've created the structure successfully, implement the click listener on the RecyclerView item, but I don't know how to show the details fragment (and how to go back from it).</p>
  
  <p>If showing another fragment is not the right way, please suggest me the best way to obtain the navigation that I need.</p>
  
  <p>CODE</p>
  
  <p>MenuActivity.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;android.support.v4.widget.DrawerLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/drawerLayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  tools:openDrawer="start"&gt;
  
  &lt;RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;android.support.v7.widget.Toolbar
          android:id="@+id/toolbar"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:background="?attr/colorPrimaryDark"
          android:theme="@style/AppTheme.AppBarOverlay"
          app:popupTheme="@style/AppTheme.PopupOverlay"
          app:titleTextColor="@android:color/white"/&gt;
  
      &lt;FrameLayout
          android:id="@+id/frameLayout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_below="@id/toolbar"/&gt;
  
  &lt;/RelativeLayout&gt;
  
  &lt;android.support.design.widget.NavigationView
      android:id="@+id/navigationView"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      android:fitsSystemWindows="true"
      app:menu="@menu/drawer_view"
      app:headerLayout="@layout/nav_header"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>MenuActivity.kt</p>
  
  <pre><code>class MenuActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
  
  val vendutoFragment = VendutoFragment()
  val prezziFragment = PrezziFragment()
  val giacenzeFragment = GiacenzeFragment()
  
  
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_menu)
      setSupportActionBar(toolbar)
  
      val toggle = ActionBarDrawerToggle(this, drawerLayout, toolbar,
              R.string.navigation_drawer_open, R.string.navigation_drawer_close)
      drawerLayout.addDrawerListener(toggle)
      toggle.syncState()
  
      navigationView.setNavigationItemSelectedListener(this)
  
      if(savedInstanceState == null){
          addFragment(vendutoFragment)
          navigationView.setCheckedItem(nav_venduto)
      }
  }
  
  override fun onBackPressed() {
      if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
          drawerLayout.closeDrawer(GravityCompat.START)
      } else {
          super.onBackPressed()
      }
  }
  
  override fun onNavigationItemSelected(item: MenuItem): Boolean {
      var selectedFragment = Fragment()
      when (item.itemId) {
          R.id.nav_venduto -&gt; {
              selectedFragment = vendutoFragment
          }
          R.id.nav_prezzi -&gt; {
              selectedFragment = prezziFragment
          }
          R.id.nav_giacenze -&gt; {
              selectedFragment = giacenzeFragment
          }
      }
  
      replaceFragment(selectedFragment)
  
      drawerLayout.closeDrawer(GravityCompat.START)
      return true
  }
  
  private fun addFragment(fragment: Fragment){
      supportFragmentManager.beginTransaction().add(R.id.frameLayout, fragment).commit()
  }
  
  private fun replaceFragment(fragment: Fragment){
      supportFragmentManager.beginTransaction().replace(R.id.frameLayout, fragment).commit()
  }
  </code></pre>
  
  <p>}</p>
  
  <p>1st Fragment with RecyclerView and clickable item</p>
  
  <pre><code>class GiacenzeFragment: Fragment(){
  var global: Global? = null
  
  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
  {
      //returning our layout file
      //change R.layout.yourlayoutfilename for each of your fragments
      return inflater.inflate(R.layout.fragment_giacenze, container, false)
  }
  
  override fun onViewCreated(view: View, savedInstanceState: Bundle?)
  {
      super.onViewCreated(view, savedInstanceState)
  
      //you can set the title for your toolbar here for different fragments different titles
      activity!!.title = "Giacenze"
  }
  
  override fun onActivityCreated(savedInstanceState: Bundle?)
  {
      super.onActivityCreated(savedInstanceState)
  
      giacenzeTable.layoutManager = LinearLayoutManager(context) as RecyclerView.LayoutManager?
  
      global = getActivity()?.getApplication() as Global
  
      // Access the RecyclerView Adapter and load the data into it
      giacenzeTable.adapter = GiacenzeTableAdapter(global!!.prodotti, { prodotto: Prodotto -&gt; prodottoItemClicked(prodotto) })
  }
  
  private fun prodottoItemClicked(prodotto: Prodotto)
  {
      Toast.makeText(context, "Clicked: ${prodotto.name}", Toast.LENGTH_SHORT).show()
  
      var serbatoiFrag =  SerbatoiFragment()
      serbatoiFrag.idProdotto = prodotto.idProdotto
      serbatoiFrag.nameProdotto = prodotto.name
  
      fragmentManager?.beginTransaction()!!.replace(R.id.frameLayout, serbatoiFrag).commit()
  
  }
  </code></pre>
  
  <p>}</p>