Calling method on navigation host fragment inside MainActivity

Madu

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.

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.

The approach I am using is the following.

val readerFragment: ReaderFragment = supportFragmentManager.findFragmentById(R.id.reader_fragment) as ReaderFragment

readerFragment.scrollToChapter(5)

Here Reader fragment is the actual fragment used underneath navigation host fragment, this results in the following exception

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

or if try to get it directly from nav_host_fragment like this

val readerFragment: ReaderFragment = nav_host_fragment as ReaderFragment

then I get the following exception

java.lang.ClassCastException: androidx.navigation.fragment.NavHostFragment cannot be cast to com.example.quran.ReaderFragment

Can somebody help me that how I can get the actual fragment from the nav_host _fragment inside the MainActivity.

Below is the code for the nav_graph

<?xml version="1.0" encoding="utf-8"?>
<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">
    <fragment
        android:id="@+id/reader_fragment"
        android:name="com.example.quran.ReaderFragment"
        android:label="Reader"
        tools:layout="@layout/reader_fragment">
    </fragment>
    <fragment
        android:id="@+id/chapterDetailsFragment"
        android:name="com.example.quran.ChapterDetailsFragment"
        android:label="fragment_chapter_details"
        tools:layout="@layout/fragment_chapter_details" />
</navigation>

and here is the layout for MainActivity

<?xml version="1.0" encoding="utf-8"?>
<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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorTopNavigation">

            <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" />

        </androidx.appcompat.widget.Toolbar>

        <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" />
    </LinearLayout>

    <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">


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/chapters_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </com.google.android.material.navigation.NavigationView>


</androidx.drawerlayout.widget.DrawerLayout>
Blundell

Explanation of your problems

Your first error from the code:

 val readerFragment: ReaderFragment = nav_host_fragment as ReaderFragment

Is because, the NavHostFragment is a host (i.e. your fragment is inside of it), so you cannot cast the NavHostFragment to anything else.

 supportFragmentManager.findFragmentById(R.id.reader_fragment) as ReaderFragment

TypeCastException: null cannot be cast to non-null type

The findFragmentById is returning null as it does not find your fragment. that's the second error.


Solutions

You can hack it a bit to get the on screen fragment:

NavHostFragment navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host);
navHostFragment.getChildFragmentManager().getFragments().get(0);

You could also check the child fragment manager since your fragment is a child of the nav host:

NavHostFragment fragment = supportFragmentManager.findFragmentById(R.id.nav_host);
MyFragment frag = (MyFragment) fragment.getChildFragmentManager().findFragmentByTag(tag);

Reference:

Android Navigation Architecture Component - Get current visible fragment

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related