title: Calling method on navigation host fragment inside MainActivity
body: <p>I am developing my first Android app and I know that architecture could be a bit messy.
  I am using new Jetpack navigation in android. I have a navigation drawer where I added a recycler view from where the user can select the item in the recycler view and based on that main navigation host fragment should scroll to a specific location.</p>
  
  <p>I wanted to know how I can call a method in the underneath fragment associated with the navigation host fragment. I can get a handle to the navigation host fragment but I wanted to call a custom method in that particular fragment which is not exposed via navigation host fragment and because of that it gives me the compile-time error.</p>
  
  <p>The approach I am using is the following.</p>
  
  <pre><code>val readerFragment: ReaderFragment = supportFragmentManager.findFragmentById(R.id.reader_fragment) as ReaderFragment
  </code></pre>
  
  <p>readerFragment.scrollToChapter(5)</p>
  
  <p>Here Reader fragment is the actual fragment used underneath navigation host fragment, this results in the following exception </p>
  
  <pre><code>com.example.quran E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.quran, PID: 7640
      kotlin.TypeCastException: null cannot be cast to non-null type com.example.quran.ReaderFragment
  </code></pre>
  
  <p>or if try to get it directly from nav_host_fragment like this </p>
  
  <pre><code>val readerFragment: ReaderFragment = nav_host_fragment as ReaderFragment
  </code></pre>
  
  <p>then I get the following exception</p>
  
  <pre><code>java.lang.ClassCastException: androidx.navigation.fragment.NavHostFragment cannot be cast to com.example.quran.ReaderFragment
  </code></pre>
  
  <p>Can somebody help me that how I can get the actual fragment from the nav_host _fragment inside the MainActivity.</p>
  
  <p>Below is the code for the nav_graph</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;navigation 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/nav_graph"
      app:startDestination="@id/reader_fragment"&gt;
      &lt;fragment
          android:id="@+id/reader_fragment"
          android:name="com.example.quran.ReaderFragment"
          android:label="Reader"
          tools:layout="@layout/reader_fragment"&gt;
      &lt;/fragment&gt;
      &lt;fragment
          android:id="@+id/chapterDetailsFragment"
          android:name="com.example.quran.ChapterDetailsFragment"
          android:label="fragment_chapter_details"
          tools:layout="@layout/fragment_chapter_details" /&gt;
  &lt;/navigation&gt;
  </code></pre>
  
  <p>and here is the layout for MainActivity</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;androidx.drawerlayout.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/drawer_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fitsSystemWindows="true"
      tools:context=".MainActivity"
      tools:openDrawer="start"&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"&gt;
  
          &lt;androidx.appcompat.widget.Toolbar
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@color/colorTopNavigation"&gt;
  
              &lt;TextView
                  android:id="@+id/toolbar_textView"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:fontFamily="@font/montserratbold"
                  android:textColor="@color/toolbarItemColor"
                  android:layout_marginRight="?android:attr/actionBarSize"
                  android:gravity="center"
                  android:textSize="30dp"
                  android:textStyle="bold" /&gt;
  
          &lt;/androidx.appcompat.widget.Toolbar&gt;
  
          &lt;fragment
              android:id="@+id/nav_host_fragment"
              android:name="androidx.navigation.fragment.NavHostFragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:defaultNavHost="true"
              app:navGraph="@navigation/nav_graph" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;com.google.android.material.navigation.NavigationView
          android:id="@+id/nav_view"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:fitsSystemWindows="true"&gt;
  
  
          &lt;androidx.recyclerview.widget.RecyclerView
              android:id="@+id/chapters_recycler_view"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" /&gt;
  
      &lt;/com.google.android.material.navigation.NavigationView&gt;
  
  
  &lt;/androidx.drawerlayout.widget.DrawerLayout&gt;
  </code></pre>