Slide 1

Slide 1 text

September 27, 2021 Dynamic Navigation in Modularized Apps 1

Slide 2

Slide 2 text

about me Github: https://github.com/sumayyah LinkedIn: https://www.linkedin.com/in/sumayyah-ahmed 2

Slide 3

Slide 3 text

Overview 30 min talk, then questions How does modularization make Navigation Components complicated? How can we solve those problems? How can we ensure that users always see a cohesive flow? 3

Slide 4

Slide 4 text

How does modularization make navigation complicated? 4

Slide 5

Slide 5 text

A B 5

Slide 6

Slide 6 text

A B startActivity() supportFragmentManager.beginTransaction() 6

Slide 7

Slide 7 text

A B findNavController().navigate( some id for B ) 7

Slide 8

Slide 8 text

A B findNavController().navigate( some id for B ) 8

Slide 9

Slide 9 text

A B findNavController().navigate(??) 9

Slide 10

Slide 10 text

Host app Library A Library B Library C 10

Slide 11

Slide 11 text

Host app Library A Library B Library C ?? ?? 11

Slide 12

Slide 12 text

Host app Library A Host Activity Feature Activity startActivity(this, FeatureActivity::class.java) 12

Slide 13

Slide 13 text

What if you can’t do a handoff? 13

Slide 14

Slide 14 text

Host app Library A Host Activity Feature Activity ?? 14

Slide 15

Slide 15 text

Host app Library A Host Activity Feature Activity Fragment A Fragment B Fragment C Graph ?? 15

Slide 16

Slide 16 text

Navigation is dynamic 16

Slide 17

Slide 17 text

Problem 1: navigating between libraries with known destinations Problem 2: navigating between libraries with unknown destinations 17

Slide 18

Slide 18 text

Problem 1: navigating between libraries with known destinations 18

Slide 19

Slide 19 text

Compile time solution: deep links 19

Slide 20

Slide 20 text

Library 20 library_nav

Slide 21

Slide 21 text

Library Host app 21 library_nav

Slide 22

Slide 22 text

Library Host app 22 library_nav

Slide 23

Slide 23 text

Library Host app 23 library_nav

Slide 24

Slide 24 text

Library Host app 24 in code: library_nav

Slide 25

Slide 25 text

how does this work? 25

Slide 26

Slide 26 text

navigate(destination: NavDestination) 26

Slide 27

Slide 27 text

NavController Nav Graph All the destinations Adds and removes destinations onto and off the backstack 27

Slide 28

Slide 28 text

XML NavController::class.java mNavGraph SparseArray NavGraph::class.java 28

Slide 29

Slide 29 text

FragmentClass::class.java Navigator Actions Deep link NavDestination XML Created by NavComponents Navigator uses FragmentTransaction Navigator uses Intents 29

Slide 30

Slide 30 text

30

Slide 31

Slide 31 text

NavDestination Navigator Knows how to navigate to this navGraph 31

Slide 32

Slide 32 text

NavDestination Navigator Knows how to navigate to this navGraph 1. Iterate through NavDestinations to find a deep link match 2. NavGraph destination knows about its own deep links 3. NavController delegates navigation to Navigator.navigate() 4. Navigator adds deep linked destination fragment to backstack 32

Slide 33

Slide 33 text

Library Host app 33 library_nav

Slide 34

Slide 34 text

How can we ensure that users always see a cohesive flow? 34

Slide 35

Slide 35 text

How do fragments in the stack react to this unknown destination? Fragment A from host app ?? – Fragment D Fragment C from host app What if Fragment C needs to know what happens in Fragment D? They don’t share a ViewModel or Activity or data store 35

Slide 36

Slide 36 text

Fragment A from host app ?? – Fragment D Fragment C from host app How can we share data across a backstack? 36

Slide 37

Slide 37 text

NavGraphViewModels 37

Slide 38

Slide 38 text

SavedStateHandle 38

Slide 39

Slide 39 text

NavBackStackEntry NavBackStackEntry NavBackStackEntry NavController SavedStateHandle ViewModelStore NavDestination Fragment Backstack 39

Slide 40

Slide 40 text

40

Slide 41

Slide 41 text

Fragment A Fragment C navController.currentBackstackEntry.savedStateHandle.set[“hasSeenNewOnboardingFlow”, false] 41

Slide 42

Slide 42 text

Fragment A Fragment D Fragment C navController.previousBackstackEntry.savedStateHandle.set[“hasSeenNewOnboardingFlow”, true] 42

Slide 43

Slide 43 text

Fragment A Fragment C navController.currentBackstackEntry.savedStateHandle.get(“hasSeenNewOnboardingFlow”) 43

Slide 44

Slide 44 text

the problem with SavedStateHandle It’s a little hacky! It requires fragments to know in advance what data is saved and in what format It’s fragile 44

Slide 45

Slide 45 text

Problem 2: navigating between libraries with unknown destinations 45

Slide 46

Slide 46 text

what if you don’t know where to go till runtime? For example: A nav destination relies on external library for content Fragment A ?? Fragment C External lib 46

Slide 47

Slide 47 text

Fragment A Fragment C Fragment D Fragment E ?? 47

Slide 48

Slide 48 text

Runtime solution: addDestination 48

Slide 49

Slide 49 text

49

Slide 50

Slide 50 text

how does this work? 50

Slide 51

Slide 51 text

Fragment A Fragment B Fragment C Fragment E NavDestination Fragment D External lib 51

Slide 52

Slide 52 text

52 Summary Navigation doesn’t have to be statically defined! Modularization creates unknowns and dynamically handled navigation can help deal with these unknowns