Skip to content Skip to sidebar Skip to footer

Android Navcontroller Hardware Back Button Crash

I am using new android storyboard to create an application. The flow need to be like following: SplashFragment -> Fragment1 -> Fragment2 Following is the storyboard(navigat

Solution 1:

I finally managed to figure out the issue and solved it.

The issue was that, I was observing a MutableLiveData from ViewModel and based on its value the navigation was happening. But I was not aware that the life cycle owner of fragment tends to destroy the Observer and reinstantiate it based on view life cycle to avoid leaks. Hence once the navigation happens, the Observer is no longer there and the code to navigate is within the observer. The same code is required on navigating back, hence when trying to access it, the code crashes.

I solved the issue by using an interface to give callback to fragment when the navigation needs to be done.


Solution 2:

Your error log is saying that you are calling an action with navcontroller with action id action_splashFragment_to_registerMSISDNFragment. Try to find that action id and check if it is valid or not. Also use app:popUpTo="@id/splashFragment" app:popUpToInclusive="true" in action_splashFragment_to_fragment1 instead of action_fragment1_to_fragment2. This will remove splash fragment from the backstack. Here is the code snippet:

<?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/launch_navigation_graph"
                app:startDestination="@id/splashFragment">

        <fragment android:id="@+id/splashFragment" android:name="com.myapp.android.SplashFragment"
                  android:label="fragment_splash" tools:layout="@layout/fragment_splash">
            <action android:id="@+id/action_splashFragment_to_fragment1"
                    app:destination="@id/fragment1"
                app:popUpTo="@id/splashFragment"
                app:popUpToInclusive="true" />
        </fragment>
        <fragment android:id="@+id/fragment1"
                  android:name="com.myapp.android.Fragment1"
                  android:label="fragment1" tools:layout="@layout/fragment_register_msisdn">
            <action android:id="@+id/action_fragment1_to_fragment2"
                    app:destination="@id/fragment2"
                    app:enterAnim="@anim/nav_default_pop_enter_anim" app:exitAnim="@anim/nav_default_pop_exit_anim"/>
        </fragment>
        <fragment android:id="@+id/fragment2"
                  android:name="com.myapp.android.Fragment2"
                  android:label="fragment_fragment2" tools:layout="@layout/fragment_fragment2"/>
    </navigation>

Solution 3:

I just removed the observer after navigating to a new fragment.Like this:

myLiveData.removeObservers(this)


Post a Comment for "Android Navcontroller Hardware Back Button Crash"