Multi-module Navigation with Architecture Components

前端 未结 3 1517
醉酒成梦
醉酒成梦 2021-01-30 21:48

So I have this structure for my modules in my current app.

I haven\'t found any official documentation on multi-module navigation yet but I found this article r

3条回答
  •  梦如初夏
    2021-01-30 22:42

    This is already a year-long but the library now can support this exact use-case! As of 2.1.0-alpha03, we can navigation through deep link URIs.

    Instead of adding the features as implementation details to each other, we can leave them unaware between themselves and use deep link navigation.

    Feature 1 - Detail - build.gradle

    dependencies {
        implementation project(':base')
    }
    

    Same with Feature 2 - Detail. No need for it to know the other modules.

    To have inter-module navigation, we have to first define the deep link for navigating through that destination via a deepLink tag.

    Feature 1 - Detail - Navigation Graph

    
        
            
    
        
    
    

    Feature 2 - Detail - Navigation Graph

    
        
            
    
        
    
    

    Now that we have deep links with URIs set, we can directly use this in a NavController

    So in the fragment in Feature 1 - Detail, maybe on a button click? Anywhere where you have to perform navigation

    class Feature1DetailFragment {
       fun onViewCreated(...) {
           ...
           view.setOnClickListener {
               val uri = Uri.parse("myApp://feature2detail")
               findNavController().navigate(uri)
           }
       }
    }
    
    

    And in Feature 2 - Detail,

    class Feature2DetailFragment {
       fun onViewCreated(...) {
           ...
           view.setOnClickListener {
               val uri = Uri.parse("myApp://feature1detail")
               findNavController().navigate(uri)
           }
       }
    }
    
    

    And voila! Inter-module navigation.

    At the time of writing, the latest stable release is 2.1.0-rc01.

    Although I haven't tried this out on more complex projects, I love this library and I'm hoping to see this library mature more!

    I created a Medium article about this. You can take a look at it. Cheers!

提交回复
热议问题