Slide 1

Slide 1 text

Android App Architecture & Navigation Bryan Herbst (@bryancherbst)

Slide 2

Slide 2 text

Activities & Fragments

Slide 3

Slide 3 text

STOP

Slide 4

Slide 4 text

STOP If you like what you have, keep it

Slide 5

Slide 5 text

When to use what?

Slide 6

Slide 6 text

Where we’ve been

Slide 7

Slide 7 text

In the beginning there was Activity

Slide 8

Slide 8 text

Activity Entry point to your application

Slide 9

Slide 9 text

Activity Entry point to your application Has a Window

Slide 10

Slide 10 text

Activity Entry point to your application Has a Window Has a lifecycle

Slide 11

Slide 11 text

Activity Entry point to your application Has a Window Has a lifecycle Responds to configuration changes

Slide 12

Slide 12 text

Activity Entry point to your application Has a Window Has a lifecycle Responds to configuration changes ActivityManager handles back stack

Slide 13

Slide 13 text

Activity Layout inflation View manipulation Click handlers Animations Navigation Menu creation Menu item callbacks Database calls Networking calls Disk I/O Sensor input Geolocation

Slide 14

Slide 14 text

Activity Window transitions can be difficult !

Slide 15

Slide 15 text

Activity Window transitions can be difficult A lot of plumbing !

Slide 16

Slide 16 text

Activity Window transitions can be difficult A lot of plumbing Nuanced (and limited) backstack !

Slide 17

Slide 17 text

Enter Honeycomb (3.0)

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Fragments!

Slide 20

Slide 20 text

Fragment Has lifecycle (less than Activity)

Slide 21

Slide 21 text

Fragment Has lifecycle (less than Activity) Backstack via FragmentManager

Slide 22

Slide 22 text

Fragment Has lifecycle (less than Activity) Backstack via FragmentManager Composable

Slide 23

Slide 23 text

Fragment Has lifecycle (less than Activity) Backstack via FragmentManager Composable Belong to an Activity

Slide 24

Slide 24 text

Fragments Transactions are annoying !

Slide 25

Slide 25 text

Fragments Transactions are annoying Lifecycles are annoying !

Slide 26

Slide 26 text

Fragments Transactions are annoying Lifecycles are annoying Can still be too big !

Slide 27

Slide 27 text

Fragments Transactions are annoying Lifecycles are annoying Can still be too big Bugs !

Slide 28

Slide 28 text

Fragments Transactions are annoying Lifecycles are annoying Can still be too big Bugs Differences from Activity !

Slide 29

Slide 29 text

Meanwhile

Slide 30

Slide 30 text

View-based architecture

Slide 31

Slide 31 text

View Draws things Reacts to user interaction

Slide 32

Slide 32 text

View Almost no lifecycle No backstack !

Slide 33

Slide 33 text

Where are we now?

Slide 34

Slide 34 text

Single activity app Easier to manage transitions

Slide 35

Slide 35 text

Single activity app Easier to manage transitions Composable UI elements

Slide 36

Slide 36 text

Single activity app Easier to manage transitions Composable UI elements Google can be opinionated

Slide 37

Slide 37 text

Don’t like lifecyles? Use Lifecycle

Slide 38

Slide 38 text

class MainActivity : Activity { override fun onStart() { super.onStart() analytics.reportStart() } }

Slide 39

Slide 39 text

activity.lifecycle .addObserver(AnalyticsObserver()) class AnalyticsObserver : LifecycleObserver { @OnLifecycleEvent(ON_START) fun trackPageLoad() { analytics.reportStart() } }

Slide 40

Slide 40 text

activity.lifecycle .addObserver(AnalyticsObserver()) class AnalyticsObserver : LifecycleObserver { @OnLifecycleEvent(ON_START) fun trackPageLoad() { analytics.reportStart() } }

Slide 41

Slide 41 text

Fragment refresher

Slide 42

Slide 42 text

Which Fragments? Framework fragments Support fragments

Slide 43

Slide 43 text

Which Fragments? Framework fragments Support fragments ! "

Slide 44

Slide 44 text

Fragment transactions

Slide 45

Slide 45 text

Fragment transactions fragmentManager.beginTransaction()

Slide 46

Slide 46 text

Fragment transactions fragmentManager.beginTransaction() .add()

Slide 47

Slide 47 text

Fragment transactions fragmentManager.beginTransaction() .remove()

Slide 48

Slide 48 text

Fragment transactions fragmentManager.beginTransaction() .replace()

Slide 49

Slide 49 text

Fragment transactions fragmentManager.beginTransaction() .show()

Slide 50

Slide 50 text

Fragment transactions fragmentManager.beginTransaction() .hide()

Slide 51

Slide 51 text

Fragment transactions fragmentManager.beginTransaction() .addToBackstack()

Slide 52

Slide 52 text

Fragment transactions fragmentManager.beginTransaction() .commit()

Slide 53

Slide 53 text

Fragment transactions fragmentManager.beginTransaction() .add() .replace() .hide() .addToBackstack() .commit()

Slide 54

Slide 54 text

Transactions are asynchronous

Slide 55

Slide 55 text

The IllegalStateException com.awesomeapp E/MainActivity: java.lang.IllegalStateException: Cannot perform this action after onSaveInstanceState

Slide 56

Slide 56 text

The IllegalStateException FM State

Slide 57

Slide 57 text

The IllegalStateException FM State add()

Slide 58

Slide 58 text

The IllegalStateException FM State add()

Slide 59

Slide 59 text

The IllegalStateException FM State add() hide()

Slide 60

Slide 60 text

The IllegalStateException FM State add() hide() onStop()!!

Slide 61

Slide 61 text

The IllegalStateException FM State add() hide() onStop()!! Persisted add() hide()

Slide 62

Slide 62 text

The IllegalStateException FM State onStart() Persisted add() hide()

Slide 63

Slide 63 text

The IllegalStateException FM State add() hide() Persisted add() hide() replace()

Slide 64

Slide 64 text

The IllegalStateException After onSaveInstanceState any future changes may be lost

Slide 65

Slide 65 text

Okay, but async transactions suck

Slide 66

Slide 66 text

Commit() commit() executePendingTransactions() commitAllowingStateLoss() commitNow() commitNowAllowingStateLoss()

Slide 67

Slide 67 text

Commit() commit() executePendingTransactions() commitAllowingStateLoss() commitNow() commitNowAllowingStateLoss()

Slide 68

Slide 68 text

Commit() commit() executePendingTransactions() commitAllowingStateLoss() commitNow() commitNowAllowingStateLoss()

Slide 69

Slide 69 text

Commit() commit() executePendingTransactions() commitAllowingStateLoss() commitNow() commitNowAllowingStateLoss()

Slide 70

Slide 70 text

Back stack addToBackStack(null)

Slide 71

Slide 71 text

Back stack popBackStack()

Slide 72

Slide 72 text

Back stack addToBackStack("screenA")

Slide 73

Slide 73 text

Back stack addToBackStack("screenA")

Slide 74

Slide 74 text

Back stack addToBackStack("screenB")

Slide 75

Slide 75 text

Back stack addToBackStack("screenC")

Slide 76

Slide 76 text

Back stack popBackStack("screenA", 0)

Slide 77

Slide 77 text

But that’s a lot of work

Slide 78

Slide 78 text

Navigation

Slide 79

Slide 79 text

Navigation Challenges Transitions

Slide 80

Slide 80 text

Navigation Challenges Transitions Back stack management

Slide 81

Slide 81 text

Navigation Challenges Transitions Back stack management Up vs. back

Slide 82

Slide 82 text

Navigation Challenges Transitions Back stack management Up vs. back Passing data

Slide 83

Slide 83 text

Navigation Challenges Transitions Back stack management Up vs. back Passing data Deep linking

Slide 84

Slide 84 text

Android Jetpack: Navigation

Slide 85

Slide 85 text

Principles Navigation is a stack

Slide 86

Slide 86 text

Principles Navigation is a stack Up never exits the app

Slide 87

Slide 87 text

Principles Navigation is a stack Up never exits the app Up = Back within your app

Slide 88

Slide 88 text

Principles Navigation is a stack Up never exits the app Up = Back within your app Deep linking should yield the same stack

Slide 89

Slide 89 text

Navigation (the library)

Slide 90

Slide 90 text

Navigation library No more Fragment transactions!

Slide 91

Slide 91 text

Navigation library No more Fragment transactions! No more startActivity()!

Slide 92

Slide 92 text

Navigation library No more Fragment transactions! No more startActivity()! Just call navigate()

Slide 93

Slide 93 text

Navigation Graph All the destinations in your app, plus the action that connect them.

Slide 94

Slide 94 text

Navigation Graph New XML resource type!

Slide 95

Slide 95 text

Navigation tooling

Slide 96

Slide 96 text

Navigator What destination types are available?

Slide 97

Slide 97 text

NavHost Where do those destinations reside?

Slide 98

Slide 98 text

NavController How do I get there?

Slide 99

Slide 99 text

Current Navigators ActivityNavigator

Slide 100

Slide 100 text

Current Navigators ActivityNavigator FragmentNavigator

Slide 101

Slide 101 text

Slide 102

Slide 102 text

Slide 103

Slide 103 text

Do the navigation Navigation .findNavController(view) .navigate(R.id.action_id)

Slide 104

Slide 104 text

Destinations: Deep link Destinations can have deep links

Slide 105

Slide 105 text

Actions: Animations Actions can have transition animations

Slide 106

Slide 106 text

Actions: back stack Each action can define its own behavior

Slide 107

Slide 107 text

Arguments Actions can all have arguments

Slide 108

Slide 108 text

Arguments Works with deep links!

Slide 109

Slide 109 text

Safeargs androidx.navigation.safeargs

Slide 110

Slide 110 text

Safeargs androidx.navigation.safeargs Generates argument wrapper classes

Slide 111

Slide 111 text

Safeargs Demo time!

Slide 112

Slide 112 text

Demo time!

Slide 113

Slide 113 text

Custom destination You can make your own Navigator!

Slide 114

Slide 114 text

What’s missing Some integration with common elements like bottom navigation

Slide 115

Slide 115 text

What’s missing Some integration with common elements like bottom navigation Shared element transitions

Slide 116

Slide 116 text

What’s missing Some integration with common elements like bottom navigation Shared element transitions Nested navigation graphs

Slide 117

Slide 117 text

What’s missing Some integration with common elements like bottom navigation Shared element transitions Nested navigation graphs Result data (e.g. startActivityForResult())

Slide 118

Slide 118 text

What if I want to migrate to Navigation?

Slide 119

Slide 119 text

I currently have…

Slide 120

Slide 120 text

Single Activity

Slide 121

Slide 121 text

Single Activity Set up navigation graph

Slide 122

Slide 122 text

Single Activity Set up navigation graph Each Fragment is a destination

Slide 123

Slide 123 text

Single Activity Set up navigation graph Each Fragment is a destination Every transaction is an action

Slide 124

Slide 124 text

Single Activity Set up navigation graph Each Fragment is a destination Every transaction is an action Define arguments

Slide 125

Slide 125 text

Single Activity Set up navigation graph Each Fragment is a destination Every transaction is an action Define arguments Replace transactions with NavController.navigate()

Slide 126

Slide 126 text

Multiple activities Start by making a graph for each Activity

Slide 127

Slide 127 text

Multiple activities Start by making a graph for each Activity If you want a single Activity, combine Activities one screen at a time

Slide 128

Slide 128 text

Activity Chrome

Slide 129

Slide 129 text

Activity Chrome CoordinatorLayout .setStatusBarBackgroundColor()

Slide 130

Slide 130 text

Activity Features

Slide 131

Slide 131 text

Activity Features activity.window .setSoftInputMode(SOFT_INPUT_ADJUST_PAN)

Slide 132

Slide 132 text

More complex Watch for updates! Provide feedback!

Slide 133

Slide 133 text

Resources https://developer.android.com /topic /libraries /architecture /navigation