Search code examples
androidkotlinnavigationandroid-architecture-navigationandroid-jetpack-navigation

Navigate from one activity to another with Navigation Component


I am trying to navigate from one navigation graph (with the host fragment on the LoginActivity) to another navigation graph (with the host fragment on the HomeActivity). I know Google is advocating for single activity applications and thus I am only going to be using these two Activities (to keep it minimal).

My login_navigation.xml

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

    <fragment
        android:id="@+id/nav_login"
        android:name="your.appname.LoginFragment"
        android:label="@string/login_menu"
        tools:layout="@layout/fragment_login" >
        <action
            android:id="@+id/action_nav_login_to_enterCellphone"
            app:destination="@id/login_enter_cellphone"
            app:popUpTo="@id/nav_login" />
    </fragment>
    <fragment
        android:id="@+id/login_enter_cellphone"
        android:name="your.appname.LoginEnterCellphoneFragment"
        android:label="@string/enter_cellphone_fragment_label"
        tools:layout="@layout/fragment_login_enter_cellphone" >
        <action
            android:id="@+id/action_enter_cellphone_to_enterOTPFragment"
            app:destination="@id/enterOTPFragment"
            app:popUpTo="@id/login_enter_cellphone" />
    </fragment>
    <fragment
        android:id="@+id/enterOTPFragment"
        android:name="your.appname.EnterOTPFragment"
        android:label="@string/enter_otp_label"
        tools:layout="@layout/fragment_enter_o_t_p" >
        <action
            android:id="@+id/action_enterOTPFragment_to_main_navigation"
            app:destination="@id/main_navigation"
            app:launchSingleTop="false" />
    </fragment>
    <include app:graph="@navigation/main_navigation" />
</navigation>

And main_navigation.xml

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

    <fragment
        android:id="@+id/nav_home"
        android:name="your.appname.HomeFragment"
        android:label="Home"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/nav_profile"
        android:name="your.appname.ProfileFragment"
        android:label="@string/profile_menu"
        tools:layout="@layout/fragment_profile" />
</navigation>

I found this answer and implemented it as follows

private fun navigateToHomeActivity() {
    val action = EnterOTPFragmentDirections.actionEnterOTPFragmentToMainNavigation()
    findNavController().navigate(action)
    (activity as LoginActivity).finish() <-- later commented out
}

but it gives me the following error E/InputMethodManager: prepareNavigationBarInfo() rootView is null

If I comment out the above line the error goes away but it only replaces the current fragment with the HomeFragment.

How do I navigate to another Activity so that the source Activity is removed (Can't navigate back to it) and the Destination Activity is used?


Solution

  • You can define 2 navigation graphs:

    • ActivityA with navigation Graph A
    • ActivityB with navigation Graph B

    In the GraphA you can define a destination to start ActivityB

    <!-- Graph A -->
    <navigation 
       ....
       <activity
            android:id="@+id/activityB"
            android:name=".ActivityB"
            ...>
    
            <argument
                .... />
    
        </activity>
    
    </navigation>
    

    Then you can navigate to that activity ( = startActivity(intent)) using it like a destination:

    Navigation.findNavController(view).navigate(R.id.activityB);
    

    More info here.