Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Breaking the Rules: Dynamic Navigation in Modularized Apps

Sumayyah Ahmed
September 02, 2022
120

Breaking the Rules: Dynamic Navigation in Modularized Apps

Properly handled navigation is critical for modularized apps, which often implement navigation logic at runtime. This presents certain challenges when working with the Navigation Components library:
* How do we determine NavDestinations that are in other libraries, without access to their NavGraphs?
* How do we change the way backstack fragments behave based on past NavDestinations?
* How do we safely maintain state for each Fragment in the backstack?

And not least - how can we solve all of these issues when working with Compose?

Add in multiple modules and multiple libraries, and writing clean navigation logic can get really messy. I’ve dealt with all of these complexities when working on heavily modularized applications. I’ll walk the audience through the lessons I learned, and how to push the Navigation library capabilities to the fullest extent.

Sumayyah Ahmed

September 02, 2022
Tweet

Transcript

  1. A B

  2. A B

  3. A B

  4. Activity NavHostFragment NavController Nav Graph XML: All the destinations in

    the flow Handles communication between view layer and nav logic
  5. Activity NavHostFragment NavController Nav Graph Handles communication between view layer

    and nav logic Compose version 1 - interoperable Composable Composable Fragment Composable Composable Fragment
  6. Composable NavHost Nav Graph Handles communication between view layer and

    nav logic Compose version 2 - 100% Compose Activity Composable Composable Composable NavHostController
  7. A B

  8. what if you have unknowns at runtime? what if navigation

    is custom to a particular user type? A->B->C is different from D->E->C?
  9. Graph A Fragment A Fragment B Fragment C Fragment A

    Activity A Graph 1 Activity B Graph 2 Fragment A Fragment B Fragment C Fragment A
  10. Graph A Fragment A Fragment B Fragment C Fragment A

    Activity A Graph 1 Activity B Graph 2 Fragment A Fragment B Fragment C Fragment A deep links
  11. array<NavDestination> <NavGraph id = R.id.graph> NavDestination Navigator<NavGraph> NavDestination NavDestination <deeplink

    uri = “uri”> <Fragment> R.id.fragmentA R.id.fragmentB NavDestination NavDestination NavDestination Navigator <Fragment>
  12. array<NavDestination> <NavGraph id = R.id.graph> NavDestination Navigator<NavGraph> NavDestination NavDestination <deeplink

    uri = “uri”> <Fragment> R.id.fragmentA R.id.fragmentB NavDestination NavDestination NavDestination Navigator <Fragment> adds this fragment to backstack
  13. what if you don’t know where to go till runtime?

    Fragment A Fragment A ?? Fragment B Module
  14. How do fragments in the stack react to this unknown

    destination? Fragment A Fragment A ?? Fragment B What if Fragment B needs to know what happens in Fragment ?? They don’t share a ViewModel or Activity or data store
  15. Fragment A Fragment A ?? Fragment B How do we

    share data across a backstack?
  16. The fundamentals of Android navigation are the same: backstack, intents,

    etc What has changed: access and responsibilities Compose doesn’t change a whole lot regarding navigation