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
9.7k
Kotlin Multiplatform
bherbst
0
660
What's in the Android Toolbox
bherbst
0
450
Gradle Deep Dive
bherbst
2
1.8k
Actions with Google
bherbst
0
1.3k
Android Transitions
bherbst
0
380
Databinding in Android
bherbst
0
390
Activities Vs. Fragments
bherbst
0
300
Android Transitions
bherbst
0
55
Other Decks in Technology
See All in Technology
【Pycon mini 東海 2024】Google Colaboratoryで試すVLM
kazuhitotakahashi
2
490
Amazon Personalizeのレコメンドシステム構築、実際何するの?〜大体10分で具体的なイメージをつかむ〜
kniino
1
100
Lexical Analysis
shigashiyama
1
150
[CV勉強会@関東 ECCV2024 読み会] オンラインマッピング x トラッキング MapTracker: Tracking with Strided Memory Fusion for Consistent Vector HD Mapping (Chen+, ECCV24)
abemii
0
220
開発生産性を上げながらビジネスも30倍成長させてきたチームの姿
kamina_zzz
2
1.6k
オープンソースAIとは何か? --「オープンソースAIの定義 v1.0」詳細解説
shujisado
3
490
強いチームと開発生産性
onk
PRO
33
11k
TypeScript、上達の瞬間
sadnessojisan
46
13k
初心者向けAWS Securityの勉強会mini Security-JAWSを9ヶ月ぐらい実施してきての近況
cmusudakeisuke
0
120
【若手エンジニア応援LT会】ソフトウェアを学んできた私がインフラエンジニアを目指した理由
kazushi_ohata
0
140
Terraform CI/CD パイプラインにおける AWS CodeCommit の代替手段
hiyanger
1
240
B2B SaaS × AI機能開発 〜テナント分離のパターン解説〜 / B2B SaaS x AI function development - Explanation of tenant separation pattern
oztick139
2
220
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
65
4.4k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Designing for humans not robots
tammielis
250
25k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Building Applications with DynamoDB
mza
90
6.1k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
730
Docker and Python
trallard
40
3.1k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
A Philosophy of Restraint
colly
203
16k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
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