Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation component with instant/(dynamic-)feature modules

I'm trying to implement the new navigation component with dynamic features

So the structure looks like this:

  • app (has dynamic dynamicFeatures = [] included in gradle)
  • features

    • login
    • home
    • etc.
  • library -> globalNav

I've been reading a lot with no success how to glue these together without depending on each other as each feature is completely isolated

e.g The app (app module) starts with the SplashActivity and checks the session state, so my question is how should I start the login_graph or home_graph and in the first case, start the home after finishing the auth flow?

The way I'm currently trying gluing'em together is using a globalNav module (android-library) I found a couple of workarounds:

  • Complete classpath, meaning I start the intent with the className (bad as this is not really scalable and makes maintaining a PITA)
Intent().setClassName(context.packageName, className).also { context.startActivity(it) }
  • Deeplinks (the one I wanted to use as my nav graph already has those) but for some reason, it always shows the "Choose complete action" picker even though only my app takes it.

    • I've already tried using actions but the same behavior as deeplinks

Any suggestions would be great :)


PS: Some references that I've been viewing/reading on:

  • https://www.youtube.com/watch?v=ST2W1Y_Ztvk

    • This vid seems to address my question with instant apps (at the end of the talk), but there's a couple of issues, time was running out so they had to rush it, and there's no model project containing this use case :()
  • https://www.youtube.com/watch?v=2k8x8V77CrU

  • https://jeroenmols.com/blog/2019/04/02/modularizationexample/

  • GitHub googlesamplesrepository

PSS: I'm actively trying to figure this out if I find a good solution I'll make sure to answer too.

EDIT

For anyone interested in this, as it isn't currently supported, you can star the issue here: https://issuetracker.google.com/issues/132170186

like image 508
Joaquim Ley Avatar asked Apr 05 '19 09:04

Joaquim Ley


People also ask

What is dynamic feature module in Android Studio?

Dynamic feature modules allow you to separate certain features and resources from the base module of your app and include them in your app bundle. Through Dynamic Delivery, users can later download and install those components on demand after they've already installed the base APK of your app.

How do I navigate a module?

Using the navigation component for a simple single-module application is easy. Just create your nav graph, set your start destination and give the nav graph to the NavHostFragment and voila, it's all ready to go!

Can navigation component be used for activities?

The Navigation component uses an activity as a host for navigation and swaps individual fragments into that host as your users navigate through your app. Before you can start to layout out your app's navigation visually, you need to configure a NavHost inside of the activity that is going to host this graph.


1 Answers

This is now supported in androidx.navigation:navigation-dynamic-features-fragment:2.3.0.

Just include app:moduleName in navigation.

Example:

<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/menu_nav_graph"
    app:startDestination="@id/main_fragment">

<!-- this fragment is in base module -->
<fragment
        android:id="@+id/main_fragment"
        android:name="com.example.app.MainFragment"
        tools:layout="@layout/fragment_main" />

<!-- activity and fragment in a different module -->
<activity
        android:id="@+id/splash_activity"
        android:name="com.example.examplemodule.splash.SplashActivity"
        app:moduleName="examplemodule" />

<fragment
        android:id="@+id/user_profile"
        android:name="com.example.examplemodule.user.UserProfileFragment"
        android:label="UserProfileFragment"
        app:moduleName="examplemodule" />

</navigation>

You can call findNavController().navigate(R.id.splash_activity) from anywhere, and it will install examplemodule for you if necessary.

like image 143
Torkel Velure Avatar answered Oct 05 '22 15:10

Torkel Velure