Android — Clean Architecture With Dynamic-Features and Hilt/Dagger2 (Pt.2)

Pedro Okawa
Mar 4 · 2 min read
Bologna — Italy (2019)

Introduction

In our previous article we setup our Gradle file and now we are going to start working on our Navigation. If you haven’t checked yet, take a look at the first part of this series.

Navigation

Our navigation file is going to include our dynamic features and here we must focus on few details otherwise, you’re going to face some issues (Trust me, I had so many issues with that).

nav_home

As you can see, by adding the dynamic navigation library we have access to the “include-dynamic” object, which requires some parameters to make it work, you may take a look at those parameters on Google’s documentation page, but a good hint for you is that you can use the “${applicationId}” to avoid mistyping the path otherwise, you must define it as:

app:graphPackage="com.okawa.blockchain.mkt.stats"
nav_pools

Inside our feature module, we must define our navigation file, and here’s one of the most important details, the id defined on our “nav_pools” must be the same created on the “nav_home”. In other words, DO NOT add the plus sign here:

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@id/nav_pools" <--- DO NOT ADD PLUS SIGN HERE
app:startDestination="@id/fragment_pools">

If you add the plus sign here you are going to declare a new id for the navigation file and it won’t match, and the result is this one:

2021-03-04 18:21:27.913 22314-22314/com.okawa.blockchain.mkt E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.okawa.blockchain.mkt, PID: 22314
java.lang.IllegalStateException: The included <navigation>'s id com.okawa.blockchain.mkt.pools:id/nav_pools is different from the destination id com.okawa.blockchain.mkt:id/nav_pools. Either remove the <navigation> id or make them match.

Now we must connect our features and in order to do so, we are going to use the bottom navigation view, first define the menu that is going to be used.

menu_bottom_home

The ids must also match here, the app will not crash if it doesn’t, but the navigation won’t take any action, as you can see in the next animation.

Non-matching ids error

Now we add the menu to our activity file and programmatically setup the bottom navigation menu using the setupWithNavController function.

activity_home
HomeActivity

Voilá!! 🎉
We now have the dynamic navigation working and for this post that’s all.
In our next article, we are going to setup Hilt and Dagger to use the dependency injection.

Any thoughts ideas or suggestions, please add some comments.

Geek Culture

Proud to geek out. Follow to join our 1M monthly readers.