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
1.9k
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
10k
Kotlin Multiplatform
bherbst
0
670
What's in the Android Toolbox
bherbst
0
460
Gradle Deep Dive
bherbst
2
1.8k
Actions with Google
bherbst
0
1.3k
Android Transitions
bherbst
0
390
Databinding in Android
bherbst
0
400
Activities Vs. Fragments
bherbst
0
310
Android Transitions
bherbst
0
55
Other Decks in Technology
See All in Technology
AI時代のデータセンターネットワーク
lycorptech_jp
PRO
1
280
Snowflake女子会#3 Snowpipeの良さを5分で語るよ
lana2548
0
220
サーバレスアプリ開発者向けアップデートをキャッチアップしてきた #AWSreInvent #regrowth_fuk
drumnistnakano
0
190
OpenAIの蒸留機能(Model Distillation)を使用して運用中のLLMのコストを削減する取り組み
pharma_x_tech
4
540
ずっと昔に Star をつけたはずの思い出せない GitHub リポジトリを見つけたい!
rokuosan
0
150
2024年にチャレンジしたことを振り返るぞ
mitchan
0
130
LINEヤフーのフロントエンド組織・体制の紹介【24年12月】
lycorp_recruit_jp
0
530
あの日俺達が夢見たサーバレスアーキテクチャ/the-serverless-architecture-we-dreamed-of
tomoki10
0
420
[Ruby] Develop a Morse Code Learning Gem & Beep from Strings
oguressive
1
150
AWS re:Invent 2024で発表された コードを書く開発者向け機能について
maruto
0
180
バクラクのドキュメント解析技術と実データにおける課題 / layerx-ccc-winter-2024
shimacos
2
1k
複雑性の高いオブジェクト編集に向き合う: プラガブルなReactフォーム設計
righttouch
PRO
0
110
Featured
See All Featured
Large-scale JavaScript Application Architecture
addyosmani
510
110k
A better future with KSS
kneath
238
17k
The Language of Interfaces
destraynor
154
24k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
Into the Great Unknown - MozCon
thekraken
33
1.5k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.9k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
Statistics for Hackers
jakevdp
796
220k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
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