简体   繁体   中英

How to navigate to Jetpack Compose function in dynamic feature module from app or another dfm?

With Fragments and Activities it's possible to open Fragment or Activity in dynamic feature module.

For instance app module can navigate to login or stage dynamic feature modules using

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

    <fragment
        android:id="@+id/mainFragment"
        android:name="com.smarttoolfactory.hopinstream.MainFragment"
        android:label="MainFragment"
        tools:layout="@layout/fragment_main">

        <action
            android:id="@+id/action_mainFragment_to_loginFragment"
            app:destination="@id/nav_graph_login"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:launchSingleTop="true"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            app:popUpTo="@id/mainFragment"
            app:popUpToInclusive="true" />

        <action
            android:id="@+id/action_mainFragment_to_stageFragment"
            app:destination="@id/nav_graph_stage"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:launchSingleTop="true"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            app:popUpTo="@id/mainFragment"
            app:popUpToInclusive="true" />

    </fragment>

    <!-- dynamic feature module-->
    <include-dynamic
        android:id="@+id/nav_graph_login"
        android:name="com.smarttoolfactory.login"
        app:graphResName="nav_graph_login"
        app:moduleName="login" />

    <include-dynamic
        android:id="@+id/nav_graph_stage"
        android:name="com.smarttoolfactory.stage"
        app:graphResName="nav_graph_stage"
        app:moduleName="stage">

        <argument
            android:name="sessionArgs"
            app:argType="com.smarttoolfactory.domain.model.UserSession" />
    </include-dynamic>

</navigation>

In login dynamic feature module navigation is as

<?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_login"
    app:moduleName="login"
    app:startDestination="@id/loginFragment">

    <fragment
        android:id="@+id/loginFragment"
        android:name="com.smarttoolfactory.login.LoginFragment"
        android:label="Login Fragment"
        tools:layout="@layout/fragment_login">
        <action
            android:id="@+id/action_loginFragment_to_stageFragment"
            app:destination="@id/nav_graph_stage"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            app:popUpTo="@id/nav_graph_login"
            app:launchSingleTop="true"
            app:popUpToInclusive="true" />
    </fragment>

    <include-dynamic
        android:id="@id/nav_graph_stage"
        android:name="com.smarttoolfactory.stage"
        app:graphResName="nav_graph_stage"
        app:moduleName="stage">

        <argument
            android:name="sessionArgs"
            app:argType="com.smarttoolfactory.domain.model.UserSession" />
    </include-dynamic>
</navigation>

in a fragment in app module it's possible to navigate via

        val direction = MainFragmentDirections
            .actionMainFragmentToLoginFragment()

        findNavController().navigate(direction)

With JetPackCompose navigation is applied for a single module app with

val navController = rememberNavController()

NavHost(navController = navController, startDestination = "profile") {
    composable("profile") { Profile(/*...*/) }
    composable("friendslist") { FriendsList(/*...*/) }
    /*...*/
}

in the code above you have each composable in app module for a single module app. Since app cannot know about feature modules is there a way to navigate from app to dynamic feature modules or between dynamic feature modules without using any Fragments as destinations?

I was looking for the same thing. Unfortunately its not available yet. There is currently a feature request for this.

issue link: https://issuetracker.google.com/issues/183677219

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM