Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Android App Architecture & Navigation
Search
Bryan Herbst
September 04, 2018
Technology
0
2k
Android App Architecture & Navigation
A look into the new navigation architecture component and how it fits with Fragments and Activites
Bryan Herbst
September 04, 2018
Tweet
Share
More Decks by Bryan Herbst
See All by Bryan Herbst
Semantics in Jetpack Compose
bherbst
0
11k
Kotlin Multiplatform
bherbst
0
700
What's in the Android Toolbox
bherbst
0
470
Gradle Deep Dive
bherbst
2
1.8k
Actions with Google
bherbst
0
1.3k
Android Transitions
bherbst
0
420
Databinding in Android
bherbst
0
410
Activities Vs. Fragments
bherbst
0
320
Android Transitions
bherbst
0
56
Other Decks in Technology
See All in Technology
君も受託系GISエンジニアにならないか
sudataka
2
430
30分でわかる『アジャイルデータモデリング』
hanon52_
9
2.7k
技術的負債解消の取り組みと専門チームのお話 #技術的負債_Findy
bengo4com
1
1.3k
速くて安いWebサイトを作る
nishiharatsubasa
10
12k
AndroidデバイスにFTPサーバを建立する
e10dokup
0
250
自動テストの世界に、この5年間で起きたこと
autifyhq
10
8.5k
明日からできる!技術的負債の返済を加速するための実践ガイド~『ホットペッパービューティー』の事例をもとに~
recruitengineers
PRO
3
390
2.5Dモデルのすべて
yu4u
2
860
レビューを増やしつつ 高評価維持するテクニック
tsuzuki817
1
710
技術負債の「予兆検知」と「状況異変」のススメ / Technology Dept
i35_267
1
1.1k
現場の種を事業の芽にする - エンジニア主導のイノベーションを事業戦略に装着する方法 -
kzkmaeda
2
2.1k
クラウドサービス事業者におけるOSS
tagomoris
1
690
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
PRO
50
11k
Designing on Purpose - Digital PM Summit 2013
jponch
117
7.1k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
Become a Pro
speakerdeck
PRO
26
5.1k
The Pragmatic Product Professional
lauravandoore
32
6.4k
Faster Mobile Websites
deanohume
306
31k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
A Modern Web Designer's Workflow
chriscoyier
693
190k
A designer walks into a library…
pauljervisheath
205
24k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
410
Raft: Consensus for Rubyists
vanstee
137
6.8k
A Philosophy of Restraint
colly
203
16k
Transcript
Android App Architecture & Navigation Bryan Herbst (@bryancherbst)
Activities & Fragments
STOP
STOP If you like what you have, keep it
When to use what?
Where we’ve been
In the beginning there was Activity
Activity Entry point to your application
Activity Entry point to your application Has a Window
Activity Entry point to your application Has a Window Has
a lifecycle
Activity Entry point to your application Has a Window Has
a lifecycle Responds to configuration changes
Activity Entry point to your application Has a Window Has
a lifecycle Responds to configuration changes ActivityManager handles back stack
Activity Layout inflation View manipulation Click handlers Animations Navigation Menu
creation Menu item callbacks Database calls Networking calls Disk I/O Sensor input Geolocation
Activity Window transitions can be difficult !
Activity Window transitions can be difficult A lot of plumbing
!
Activity Window transitions can be difficult A lot of plumbing
Nuanced (and limited) backstack !
Enter Honeycomb (3.0)
None
Fragments!
Fragment Has lifecycle (less than Activity)
Fragment Has lifecycle (less than Activity) Backstack via FragmentManager
Fragment Has lifecycle (less than Activity) Backstack via FragmentManager Composable
Fragment Has lifecycle (less than Activity) Backstack via FragmentManager Composable
Belong to an Activity
Fragments Transactions are annoying !
Fragments Transactions are annoying Lifecycles are annoying !
Fragments Transactions are annoying Lifecycles are annoying Can still be
too big !
Fragments Transactions are annoying Lifecycles are annoying Can still be
too big Bugs !
Fragments Transactions are annoying Lifecycles are annoying Can still be
too big Bugs Differences from Activity !
Meanwhile
View-based architecture
View Draws things Reacts to user interaction
View Almost no lifecycle No backstack !
Where are we now?
Single activity app Easier to manage transitions
Single activity app Easier to manage transitions Composable UI elements
Single activity app Easier to manage transitions Composable UI elements
Google can be opinionated
Don’t like lifecyles? Use Lifecycle
class MainActivity : Activity { override fun onStart() { super.onStart()
analytics.reportStart() } }
activity.lifecycle .addObserver(AnalyticsObserver()) class AnalyticsObserver : LifecycleObserver { @OnLifecycleEvent(ON_START) fun trackPageLoad()
{ analytics.reportStart() } }
activity.lifecycle .addObserver(AnalyticsObserver()) class AnalyticsObserver : LifecycleObserver { @OnLifecycleEvent(ON_START) fun trackPageLoad()
{ analytics.reportStart() } }
Fragment refresher
Which Fragments? Framework fragments Support fragments
Which Fragments? Framework fragments Support fragments ! "
Fragment transactions
Fragment transactions fragmentManager.beginTransaction()
Fragment transactions fragmentManager.beginTransaction() .add()
Fragment transactions fragmentManager.beginTransaction() .remove()
Fragment transactions fragmentManager.beginTransaction() .replace()
Fragment transactions fragmentManager.beginTransaction() .show()
Fragment transactions fragmentManager.beginTransaction() .hide()
Fragment transactions fragmentManager.beginTransaction() .addToBackstack()
Fragment transactions fragmentManager.beginTransaction() .commit()
Fragment transactions fragmentManager.beginTransaction() .add() .replace() .hide() .addToBackstack() .commit()
Transactions are asynchronous
The IllegalStateException com.awesomeapp E/MainActivity: java.lang.IllegalStateException: Cannot perform this action after
onSaveInstanceState
The IllegalStateException FM State
The IllegalStateException FM State add()
The IllegalStateException FM State add()
The IllegalStateException FM State add() hide()
The IllegalStateException FM State add() hide() onStop()!!
The IllegalStateException FM State add() hide() onStop()!! Persisted add() hide()
The IllegalStateException FM State onStart() Persisted add() hide()
The IllegalStateException FM State add() hide() Persisted add() hide() replace()
The IllegalStateException After onSaveInstanceState any future changes may be lost
Okay, but async transactions suck
Commit() commit() executePendingTransactions() commitAllowingStateLoss() commitNow() commitNowAllowingStateLoss()
Commit() commit() executePendingTransactions() commitAllowingStateLoss() commitNow() commitNowAllowingStateLoss()
Commit() commit() executePendingTransactions() commitAllowingStateLoss() commitNow() commitNowAllowingStateLoss()
Commit() commit() executePendingTransactions() commitAllowingStateLoss() commitNow() commitNowAllowingStateLoss()
Back stack addToBackStack(null)
Back stack popBackStack()
Back stack addToBackStack("screenA")
Back stack addToBackStack("screenA")
Back stack addToBackStack("screenB")
Back stack addToBackStack("screenC")
Back stack popBackStack("screenA", 0)
But that’s a lot of work
Navigation
Navigation Challenges Transitions
Navigation Challenges Transitions Back stack management
Navigation Challenges Transitions Back stack management Up vs. back
Navigation Challenges Transitions Back stack management Up vs. back Passing
data
Navigation Challenges Transitions Back stack management Up vs. back Passing
data Deep linking
Android Jetpack: Navigation
Principles Navigation is a stack
Principles Navigation is a stack Up never exits the app
Principles Navigation is a stack Up never exits the app
Up = Back within your app
Principles Navigation is a stack Up never exits the app
Up = Back within your app Deep linking should yield the same stack
Navigation (the library)
Navigation library No more Fragment transactions!
Navigation library No more Fragment transactions! No more startActivity()!
Navigation library No more Fragment transactions! No more startActivity()! Just
call navigate()
Navigation Graph All the destinations in your app, plus the
action that connect them.
Navigation Graph New XML resource type!
Navigation tooling
Navigator What destination types are available?
NavHost Where do those destinations reside?
NavController How do I get there?
Current Navigators ActivityNavigator
Current Navigators ActivityNavigator FragmentNavigator
<fragment android:id="@+id/navHost" android:name= "androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:navGraph="@navigation/nav_graph" />
<fragment android:id="@+id/navHost" android:name= "androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:navGraph="@navigation/nav_graph" />
Do the navigation Navigation .findNavController(view) .navigate(R.id.action_id)
Destinations: Deep link Destinations can have deep links <fragment android:id="@+id/home”
android:name="com.example.HomeFragment" android:label="fragment_home"> <deepLink app:uri="http://example.com/detail"/> </fragment>
Actions: Animations Actions can have transition animations <action android:id="@+id/action_home_to_detail" app:destination="@id/detail"
app:popEnterAnim="@anim/slide_in_left" app:popExitAnim="@anim/slide_out_right" app:enterAnim="@anim/slide_in_right" app:exitAnim="@anim/slide_out_left"/>
Actions: back stack Each action can define its own behavior
<action android:id="@+id/home_to_details" app:destination="@id/details” app:popUpTo="@id/splash” app:popUpToInclusive="false" />
Arguments Actions can all have arguments <fragment> <argument android:name="name" android:defaultValue="Joe"
/> </fragment>
Arguments Works with deep links! <fragment> <argument android:name="name" android:defaultValue="Joe" />
<deepLink app:uri="example.com/{name}" /> </fragment>
Safeargs androidx.navigation.safeargs
Safeargs androidx.navigation.safeargs Generates argument wrapper classes
Safeargs Demo time!
Demo time!
Custom destination You can make your own Navigator!
What’s missing Some integration with common elements like bottom navigation
What’s missing Some integration with common elements like bottom navigation
Shared element transitions
What’s missing Some integration with common elements like bottom navigation
Shared element transitions Nested navigation graphs
What’s missing Some integration with common elements like bottom navigation
Shared element transitions Nested navigation graphs Result data (e.g. startActivityForResult())
What if I want to migrate to Navigation?
I currently have…
Single Activity
Single Activity Set up navigation graph
Single Activity Set up navigation graph Each Fragment is a
destination
Single Activity Set up navigation graph Each Fragment is a
destination Every transaction is an action
Single Activity Set up navigation graph Each Fragment is a
destination Every transaction is an action Define arguments
Single Activity Set up navigation graph Each Fragment is a
destination Every transaction is an action Define arguments Replace transactions with NavController.navigate()
Multiple activities Start by making a graph for each Activity
Multiple activities Start by making a graph for each Activity
If you want a single Activity, combine Activities one screen at a time
Activity Chrome
Activity Chrome CoordinatorLayout .setStatusBarBackgroundColor()
Activity Features <activity android:name=".MainActivity" android:windowSoftInputMode="adjustPan" />
Activity Features <activity android:name=".MainActivity" android:windowSoftInputMode="adjustPan" /> activity.window .setSoftInputMode(SOFT_INPUT_ADJUST_PAN)
More complex Watch for updates! Provide feedback!
Resources https://developer.android.com /topic /libraries /architecture /navigation