Slide 1

Slide 1 text

Navigation Architecture Component Daniel Hartwich GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 1

Slide 2

Slide 2 text

Agenda • Principles of Navigation • Implement navigation using architecture component + Demo • Migrating existing projects • Implement support for new destinations • Conditional navigation • Global actions / common destinations GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 2

Slide 3

Slide 3 text

Principles of navigation GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 3

Slide 4

Slide 4 text

Principles of navigation "The app should have a fixed starting destination" GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 4

Slide 5

Slide 5 text

Principles of navigation "A stack is used to represent the "navigation state" of an app" GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 5

Slide 6

Slide 6 text

Principles of navigation "The Up button never exits your app" GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 6

Slide 7

Slide 7 text

Principles of navigation "Up and Back are equivalent within your app's task" GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 7

Slide 8

Slide 8 text

Principles of navigation "Deep linking to a destination or navigating to the same destination should yield the same stack" GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 8

Slide 9

Slide 9 text

Implement navigation using architecture component GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 9

Slide 10

Slide 10 text

Implement navigation using architecture component • Simplify navigation between destinations of your app • Set of destinations compose app's navigation graph • A destination is any place you can navigate to in your app GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 10

Slide 11

Slide 11 text

Implement navigation using architecture component • Destinations are usually fragments • Activities • different navigation graphs / subgraphs • Custom destination types • Connections between different destinations are called actions GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 11

Slide 12

Slide 12 text

Example Navigation Graph GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 12

Slide 13

Slide 13 text

Demo Set up navigation component in Android Studio Android Studio https://developer.android.com/topic/libraries/architecture/ navigation/navigation-implementing GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 13

Slide 14

Slide 14 text

Add the dependencies to the navigation component implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha05' implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha05' implementation 'android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha05' implementation 'android.arch.navigation:navigation-ui-ktx:1.0.0-alpha05' GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 14

Slide 15

Slide 15 text

Declare an activity as the Navigation Host activity_main.xml GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 15

Slide 16

Slide 16 text

Create blank destination • In the "Design" Tab of the navigation resource. Click on 'Create blank destination' GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 16

Slide 17

Slide 17 text

Create links between destinations • Create actions by dragging connections between two destinations GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 17

Slide 18

Slide 18 text

Use Actions to navigate between two destinations • Create actions by dragging connections between two destinations GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 18

Slide 19

Slide 19 text

Call the actions to execute navigation view.seeArticlesButton.setOnClickListener { view.findNavController().navigate(R.id.action_lastPurchasesFragment_to_articleDetailFragment) } or view.seeArticlesButton.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.overviewFragment)) GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 19

Slide 20

Slide 20 text

Migrating existing projects GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 20

Slide 21

Slide 21 text

Migrating existing projects • NavController + navigation graph are contained within a single activity • When migrating, migrate one activity at a time • To migrate create a navigation graph for all destinations inside an activity GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 21

Slide 22

Slide 22 text

Migrating existing projects • Add activity destinations in the navigation graph • Replace existing usages of startActivity() • Ideally, migrate activities to fragments • --> Single Activity Applications GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 22

Slide 23

Slide 23 text

Implement support for new destinations GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 23

Slide 24

Slide 24 text

Implement support for new destinations • NavControllers rely on one or more Navigator objects to perform navigation. • By default only Activities and Fragments are supported • Subclass Navigator to implement navigation to your custom destination • Use getNavigatorProvider() to add your custom Navigator using addNavigator() GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 24

Slide 25

Slide 25 text

Implement support for new destinations val customNavigator = CustomNavigator() navController.navigatorProvider += customNavigator • Most Navigator classes have a nested destination subclass • It's used to specify additional attributes unique to the custom destination GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 25

Slide 26

Slide 26 text

Conditional navigation GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 26

Slide 27

Slide 27 text

Conditional navigation • Destinations that can only be reached under certain conditions should still be an own destination • e.g. Profile screen behind a Log In mechanic • Login is presented after the user tries to access 'Profile' without log in • After login is completed we call popBackStack() to dismiss the login screen and show the profile again. GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 27

Slide 28

Slide 28 text

Global actions / common destinations • An action that is accessible from multiple screens • Example: A 'cancel' button in a multi step process that should go back to the first screen • Right Click a destination and add a global action. • This action is now accessible from everywhere and can be called the same way as other actions. GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 28

Slide 29

Slide 29 text

Navigating to a global action viewTransactionButton.setOnClickListener { view -> view.findNavController().navigate(R.id.action_global_mainFragment) } GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 29

Slide 30

Slide 30 text

Additional notes • Unit Tests not possible, without further abstraction • Compatible with known architecture patterns • It's possible to navigate between two modules through in the navigation graph GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 30

Slide 31

Slide 31 text

Additional notes • Support for navigation drawer and overflow menu possible through matching IDs (fragment + menu.xml) Set up the navigation view with the NavController: val navigationView = findViewById(R.id.nav_view) navigationView.setupWithNavController(navController) GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 31

Slide 32

Slide 32 text

Sources • Official documentation • Medium article about multi module navigation • dhartwich1991/navigation-component • googlesamples/android-sunflower • NavController documentation GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 32

Slide 33

Slide 33 text

Questions? GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 33

Slide 34

Slide 34 text

Thank you! GDG September Meetup 04.09.2018 at XING - Navigation Architecture Component - Daniel Hartwich - @KiLLyA_ 34