Monday, 6 March 2023

How to reuse Fragments within a Nav Graph in a multi-module architecture?

Context

I have a multi-module architecture, with multiple feature modules, it kinda looks like this:

enter image description here

I have multiple feature modules that depend on a :core_library library module that contains all the common dependencies (Retrofit, Room, etc.) and then different feature modules for each of the different app flows. Finally, the :app application module ties everything together.

If you want to navigate between Activities in feature modules that don't know anything about each other I use an AppNavigator interface:

interface AppNavigator {
   fun provideActivityFromFeatureModuleA(context: Context): Intent
}

Then in the :app module Application class I implement this interface, and since the :app module ties everything together it knows each of the activities within each of the feature modules:

class MyApp : Application(), AppNavigator {
...
   override fun provideActivityFromFeatureModuleA(context: Context): Intent {
      return Intent(context, ActivityFromA::class.java)
   }
...
}

This AppNavigator component lives in a Dagger module up in :core_library and it can be injected in any feature module.

I have this :feature_login feature module that is for when the user creates a new account and has to go through the onboarding flow, things like inviting friends to join the app, checking for POST_NOTIFICATION permissions, adding any more details to its account, etc.

Each of the :feature_modules has one Activity and many Fragments I have a navigation graph to navigate between fragments.

The problem

The :feature_login navigation graph kinda looks like this:

enter image description here

The thing is that I need to reuse many of these Fragments across different parts of the App, more specifically, these Fragments

enter image description here

For example; When I open the app and land on the main screen, I check for POST_NOTIFICATION permissions, and if these haven't been granted, I want to prompt the PostNotificationFragment that checks for that and presents the user with a UI. The SelectSquadronFragment + SelectNumberFragment should be prompted if the user wants to change them from the Settings screen. When doing something I want to prompt the user with the InviteFriendsFragment.

The problem is that I don't know how to reuse these Fragments independently without having them navigate through the rest of the flow

What I have tried so far

  • Subgraphs don't really fix the issue. I can use the AppNavigator to either provide the hosting Activity I have in :feature_login or each individual Fragment, but the issue is still there. If the user opens SelectSquadronFragment + SelectNumberFragment from Settings, I don't want the user to have to go through FinishFragment afterward.

  • Extracting the navigation through an interface up to the Activity. Each Fragment in that navigation graph navigates through NavDirections. When I want to navigate from MedictFragment to InviteFriendsFragment I use MedicFragmentDirections. I was thinking about having the Activity provide these NavDirections, that way I could create customized Activities with the navigation routes that I want, but honestly, I would prefer to go with something that isn't that rocket science.

Please let me know if you need me to give you more info. Any feedback is welcome.



from How to reuse Fragments within a Nav Graph in a multi-module architecture?

No comments:

Post a Comment