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
690
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
410
Databinding in Android
bherbst
0
400
Activities Vs. Fragments
bherbst
0
320
Android Transitions
bherbst
0
56
Other Decks in Technology
See All in Technology
【NGK2025S】動物園(PINTO_model_zoo)に遊びに行こう
kazuhitotakahashi
0
220
dbtを中心にして組織のアジリティとガバナンスのトレードオンを考えてみた
gappy50
0
240
技術に触れたり、顔を出そう
maruto
1
150
WantedlyでのKotlin Multiplatformの導入と課題 / Kotlin Multiplatform Implementation and Challenges at Wantedly
kubode
0
250
Accessibility Inspectorを活用した アプリのアクセシビリティ向上方法
hinakko
0
180
データ基盤におけるIaCの重要性とその運用
mtpooh
4
500
AWS re:Invent 2024 re:Cap Taipei (for Developer): New Launches that facilitate Developer Workflow and Continuous Innovation
dwchiang
0
160
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
6
54k
AWS re:Invent 2024 recap in 20min / JAWSUG 千葉 2025.1.14
shimy
1
100
ゼロからわかる!!AWSの構成図を書いてみようワークショップ 問題&解答解説 #デッカイギ #羽田デッカイギおつ
_mossann_t
0
1.5k
Copilotの力を実感!3ヶ月間の生成AI研修の試行錯誤&成功事例をご紹介。果たして得たものとは・・?
ktc_shiori
0
350
re:Invent2024 KeynoteのAmazon Q Developer考察
yusukeshimizu
1
150
Featured
See All Featured
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Code Review Best Practice
trishagee
65
17k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.8k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
30
2.1k
KATA
mclloyd
29
14k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.2k
Optimising Largest Contentful Paint
csswizardry
33
3k
Fireside Chat
paigeccino
34
3.1k
Building Better People: How to give real-time feedback that sticks.
wjessup
366
19k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
GitHub's CSS Performance
jonrohan
1030
460k
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