Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
390
Activities Vs. Fragments
bherbst
0
310
Android Transitions
bherbst
0
55
Other Decks in Technology
See All in Technology
リクルートのデータ基盤 Crois 年3倍成長!1日40,000コンテナの実行を支える AWS 活用とプラットフォームエンジニアリング
recruitengineers
PRO
1
210
GitHub Actions의 다양한 기능 활용하기 - GitHub Universe '24 Recap
outsider
0
510
Reliability Engineering at Studist
katsuhisa91
PRO
0
120
つくってあそぼ! ユビキタス言語作文の紹介
ndadayo
1
150
イベントをどう管理するか
mikanichinose
1
120
大規模サーバ移行を成功に導くための事前調査フェーズの工夫事例
fukuchiiinu
2
110
プロダクトマネージャーは 事業責任者の夢をみるのか pmconf2024
gimupop
2
8.9k
ファインディの4年にわたる技術的負債の返済 / Repaying 4 Years of Technical Debt at Findy
ma3tk
7
3.8k
Advancing the 3D Geospatial Ecosystem in Japan via Global Collaborations
osgeojp
0
180
Kubernetesトラフィックルーティング徹底解説/Kubernetes-traffic-deep-dive
oracle4engineer
PRO
3
230
宇宙最速のランチRecap LT会(AWS re:Invent 2024)
watany
1
380
Amazon Bedrock Multi-Agent Collaboration Workshop の紹介 - ワークショップでAIエージェントを学ぼう
nasuvitz
3
290
Featured
See All Featured
4 Signs Your Business is Dying
shpigford
181
21k
Building Adaptive Systems
keathley
38
2.3k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.3k
Gamification - CAS2011
davidbonilla
80
5.1k
A designer walks into a library…
pauljervisheath
204
24k
Designing for Performance
lara
604
68k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.8k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
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