Slide 1

Slide 1 text

NAVIGATION ARCHITECTURE COMPONENT Wahib-Ul-Haq @wahibhaq @DevRelMunich GDG Munich Android 04.09.2019

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Totally appreciate efforts of the team behind “Navigation” which is trying to make things easier for developers along with providing more clarity on principles

Slide 7

Slide 7 text

Announced in Google I/O 2018 Reached stable version in March 2019

Slide 8

Slide 8 text

The Navigation Architecture Component is designed for apps that have…

Slide 9

Slide 9 text

The Navigation Architecture Component is designed for apps that have… one main activity with multiple fragment destinations

Slide 10

Slide 10 text

In an app with multiple activity destinations, each additional activity hosts its own navigation graph

Slide 11

Slide 11 text

MOTIVATION

Slide 12

Slide 12 text

Designed to enforce the “Principles of Navigation”

Slide 13

Slide 13 text

Designed to enforce the “Principles of Navigation” Explicit about handling of “Up” ( navigateUp() ) and “Back” ( popBackStack() )

Slide 14

Slide 14 text

Designed to enforce the “Principles of Navigation” Explicit about handling of “Up” ( navigateUp() ) and “Back” ( popBackStack() ) Visual navigation graph for easy overview

Slide 15

Slide 15 text

Designed to enforce the “Principles of Navigation” Explicit about handling of “Up” ( navigateUp() ) and “Back” ( popBackStack() ) Visual navigation graph for easy overview Allows you to skip implementing FragmentTransactions

Slide 16

Slide 16 text

Designed to enforce the “Principles of Navigation” Explicit about handling of “Up” ( navigateUp() ) and “Back” ( popBackStack() ) Visual navigation graph for easy overview Allows you to skip implementing FragmentTransactions Type-safety for arguments when using SafeArgs

Slide 17

Slide 17 text

Designed to enforce the “Principles of Navigation” Explicit about handling of “Up” ( navigateUp() ) and “Back” ( popBackStack() ) Visual navigation graph for easy overview Allows you to skip implementing FragmentTransactions Type-safety for arguments when using SafeArgs Conditional navigation is possible

Slide 18

Slide 18 text

Designed to enforce the “Principles of Navigation” Explicit about handling of “Up” ( navigateUp() ) and “Back” ( popBackStack() ) Visual navigation graph for easy overview Allows you to skip implementing FragmentTransactions Type-safety for arguments when using SafeArgs Conditional navigation is possible Easy handling of deep links with synthetic back stack for “Up” navigation

Slide 19

Slide 19 text

Designed to enforce the “Principles of Navigation” Explicit about handling of “Up” ( navigateUp() ) and “Back” ( popBackStack() ) Visual navigation graph for easy overview Allows you to skip implementing FragmentTransactions Type-safety for arguments when using SafeArgs Conditional navigation is possible Easy handling of deep links with synthetic back stack for “Up” navigation Works with Navigation UI Patterns like NavigationView and BottomNavigationView

Slide 20

Slide 20 text

Designed to enforce the “Principles of Navigation” Explicit about handling of “Up” ( navigateUp() ) and “Back” ( popBackStack() ) Visual navigation graph for easy overview Allows you to skip implementing FragmentTransactions Type-safety for arguments when using SafeArgs Conditional navigation is possible Easy handling of deep links with synthetic back stack for “Up” navigation Works with Navigation UI Patterns like NavigationView and BottomNavigationView Testing

Slide 21

Slide 21 text

BUILDING BLOCKS

Slide 22

Slide 22 text

NavHostFragment NavHost is an empty container where destinations are swapped in and out as a user navigates through your app NavHostFragment is the default NavHost implementation and handles swapping fragment destinations

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Graph NavGraph is a collection of destinations and actions It can be defined as xml file or created programmatically You can have multiple navigation graphs in your application and they can be nested

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

https://developer.android.com/guide/navigation/

Slide 30

Slide 30 text

https://developer.android.com/guide/navigation/

Slide 31

Slide 31 text

Destinations Individual screens are referred as destinations which could be Fragments or Activities StartDestination is the first screen in the flow or graph Activity destinations are considered terminal destinations

Slide 32

Slide 32 text

NavController NavController, an object that manages app navigation within a NavHost and is used to navigate to a destination

Slide 33

Slide 33 text

NavController NavController, an object that manages app navigation within a NavHost and is used to navigate to a destination When you call navigate(), it handles the rest

Slide 34

Slide 34 text

NavController NavController, an object that manages app navigation within a NavHost and is used to navigate to a destination When you call navigate(), it handles the rest Various static methods to retrieve NavController

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

NavController NavController, an object that manages app navigation within a NavHost and is used to navigate to a destination When you call navigate(), it handles the rest Various static methods to retrieve NavController Each NavHost has its own corresponding NavController

Slide 37

Slide 37 text

NavController NavController, an object that manages app navigation within a NavHost and is used to navigate to a destination When you call navigate(), it handles the rest Various static methods to retrieve NavController Each NavHost has its own corresponding NavController Prefer navigating via Actions over Destination Ids

Slide 38

Slide 38 text

https://developer.android.com/guide/navigation/

Slide 39

Slide 39 text

Actions Configuration for each Action includes destination, default arguments and navigation options With Safe-Args, *FragmentDirections class and *FragmentArgs class is generated Safe-Args require plugin "androidx.navigation.safeargs.kotlin" Global action enables reusable navigation and is defined at root level of graph (E.g navigating to an external activity)

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

Deep Links Deep links can be created programmatically (only explicit ones*)

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Deep Links Deep links can be created programmatically (only explicit ones*) Add reference in the Manifest under the host activity for Implicit ones (and external access)

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

Deep Links Deep links can be created programmatically (only explicit ones*) Add reference in the Manifest under the host activity for Implicit ones (and external access) URIs without a scheme are assumed as http and https

Slide 50

Slide 50 text

Deep Links Deep links can be created programmatically (only explicit ones*) Add reference in the Manifest under the host activity for Implicit ones (and external access) URIs without a scheme are assumed as http and https Scheme needs to be explicitly defined

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Deep Links Deep links can be created programmatically (only explicit ones*) Add reference in the Manifest under the host activity for Implicit ones (and external access) URIs without a scheme are assumed as http and https Scheme needs to be explicitly defined Possible to define multiple implicit ones for single destination

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Deep Links Deep links can be created programmatically (only explicit ones*) Add reference in the Manifest under the host activity for Implicit ones (and external access) URIs without a scheme are assumed as http and https Scheme needs to be explicitly defined Possible to define multiple implicit ones for single destination Navigate programmatically using an implicit deep link

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Note: When navigating using URI, the back stack is not reset.

Slide 57

Slide 57 text

FUN STUFF

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

Conditional Navigation https://github.com/gogeta95/NavigationSplashScreen

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

https://developer.android.com/guide/navigation/navigation-conditional

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

Deep Links To handle the deep linking we need to add intent filter to our AndroidManifest.xml Deep links can be created programmatically Doc says that adding should be enough but I still needed to add intent filters explicitly in the Manifest

Slide 64

Slide 64 text

Deep Links

Slide 65

Slide 65 text

https://medium.com/swlh/using-the-navigation-component-in-a-modular-world-e7578825962

Slide 66

Slide 66 text

Multi-Graph Navigation

Slide 67

Slide 67 text

Multi-Graph Navigation Nested graph: Sub-group of destinations which allow reusing section of app’s UI

Slide 68

Slide 68 text

Multi-Graph Navigation Nested graph: Sub-group of destinations which allow reusing section of app’s UI Navigating between graphs across modules is possible

Slide 69

Slide 69 text

Multi-Graph Navigation Nested graph: Sub-group of destinations which allow reusing section of app’s UI Navigating between graphs across modules is possible To navigate from Feature One to Feature Two graph, we can navigate using the deep link URI

Slide 70

Slide 70 text

Multi-Graph Navigation Nested graph: Sub-group of destinations which allow reusing section of app’s UI Navigating between graphs across modules is possible To navigate from Feature One to Feature Two graph, we can navigate using the deep link URI Main app graph can include the feature module's graphs using tag

Slide 71

Slide 71 text

https://medium.com/swlh/using-the-navigation-component-in-a-modular-world-e7578825962

Slide 72

Slide 72 text

Multi-Graph Multi-Module Navigation (#1) Each module has its own graph with all the Fragments but without Activity Single Activity defined in app module Each module refers to a bottom nav item

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

Multi-Graph Multi-Module Navigation (#2) https://github.com/PhilippeBoisney/ArchApp

Slide 75

Slide 75 text

Multi-Graph Multi-Module Navigation (#2) Graphs are defined in a separate navigation module App module depends on navigation and includes Host Activity Each module contains Fragments and other necessary classes

Slide 76

Slide 76 text

Working with Bottom Nav and Multi-module Simpler Implementation: https://github.com/kuuuurt/rick-and-morty-app Advance Implementation: https://github.com/googlesamples/android-architecture-components/tree/master/NavigationAdva ncedSample

Slide 77

Slide 77 text

Will it work if destinations are Custom Views?

Slide 78

Slide 78 text

Will it work if destinations are Custom Views? Yes, it is possible to add new Destination Type To add a custom destination, you have to implement the Navigator interface https://developer.android.com/guide/navigation/navigation-add-new

Slide 79

Slide 79 text

https://www.raywenderlich.com/6014-the-navigation-architecture-component-tutorial-getting-started#toc-anchor-016

Slide 80

Slide 80 text

Will it work if destinations are Custom Views? Yes, it is possible to add new Destination Type To add a custom destination, you have to implement the Navigator interface You pass it to the NavigationController when initializing the navigation graph https://developer.android.com/guide/navigation/navigation-add-new

Slide 81

Slide 81 text

https://www.raywenderlich.com/6014-the-navigation-architecture-component-tutorial-getting-started#toc-anchor-016

Slide 82

Slide 82 text

CURRENT CHALLENGE

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

How to make Deep Links work with dynamic scheme

Slide 85

Slide 85 text

How to make Deep Links work with dynamic scheme

Slide 86

Slide 86 text

Learnings Be vigilant and have a plan when you are refactoring/rewriting a flow inside existing codebase

Slide 87

Slide 87 text

Learnings Be vigilant and have a plan when you are refactoring/rewriting a flow inside existing codebase Gladly, there is a migration doc

Slide 88

Slide 88 text

Learnings Be vigilant and have a plan when you are refactoring/rewriting a flow inside existing codebase Gladly, there is a migration doc Codelabs implementation might be outdated

Slide 89

Slide 89 text

Learnings Be vigilant and have a plan when you are refactoring/rewriting a flow inside existing codebase Gladly, there is a migration doc Codelabs implementation might be outdated Expect implementing transient solution if unable to use alpha or release candidate (e.g custom back navigation via OnBackPressedCallback)

Slide 90

Slide 90 text

Learnings Be vigilant and have a plan when you are refactoring/rewriting a flow inside existing codebase Gladly, there is a migration doc Codelabs implementation might be outdated Expect implementing transient solution if unable to use alpha or release candidate (e.g custom back navigation via OnBackPressedCallback) Integrating Deep link in existing implementation could be a challenge, might need nested deep links

Slide 91

Slide 91 text

Learnings Be vigilant and have a plan when you are refactoring/rewriting a flow inside existing codebase Gladly, there is a migration doc Codelabs implementation might be outdated Expect implementing transient solution if unable to use alpha or release candidate (e.g custom back navigation via OnBackPressedCallback) Integrating Deep link in existing implementation could be a challenge, might need nested deep links Dynamic Feature Navigation is yet to be included in the library (work-in-progress)

Slide 92

Slide 92 text

Learnings Be vigilant and have a plan when you are refactoring/rewriting a flow inside existing codebase Gladly, there is a migration doc Codelabs implementation might be outdated Expect implementing transient solution if unable to use alpha or release candidate (e.g custom back navigation via OnBackPressedCallback) Integrating Deep link in existing implementation could be a challenge, might need nested deep links Dynamic Feature Navigation is yet to be included in the library (work-in-progress) Synthetic back navigation might not work out of the box with deep link

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

Thanks for listening! @wahibhaq @DevRelMunich