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
安全にNavigateしよう
Search
ゆつぼ
April 21, 2023
Technology
0
540
安全にNavigateしよう
Shibuya.apk#41
ゆつぼ
April 21, 2023
Tweet
Share
More Decks by ゆつぼ
See All by ゆつぼ
Glance 3分クッキング
morayl
0
110
CredentialManager移行の落とし穴
morayl
0
1.4k
DAI語で行こう #as_tips / daigo_as_tips
morayl
1
510
Other Decks in Technology
See All in Technology
予期せぬコストの急増を障害のように扱う――「コスト版ポストモーテム」の導入とその後の改善
muziyoshiz
1
2.1k
M&A 後の統合をどう進めるか ─ ナレッジワーク × Poetics が実践した組織とシステムの融合
kworkdev
PRO
1
550
1,000 にも届く AWS Organizations 組織のポリシー運用をちゃんとしたい、という話
kazzpapa3
0
210
AzureでのIaC - Bicep? Terraform? それ早く言ってよ会議
torumakabe
1
640
We Built for Predictability; The Workloads Didn’t Care
stahnma
0
150
30万人の同時アクセスに耐えたい!新サービスの盤石なリリースを支える負荷試験 / SRE Kaigi 2026
genda
4
1.4k
SREチームをどう作り、どう育てるか ― Findy横断SREのマネジメント
rvirus0817
0
380
プロポーザルに込める段取り八分
shoheimitani
1
720
Prox Industries株式会社 会社紹介資料
proxindustries
0
110
コンテナセキュリティの最新事情 ~ 2026年版 ~
kyohmizu
7
2.6k
#23 Turing × atmaCup 2nd 6th Place Solution + 取り組み方紹介
yumizu
0
100
20260204_Midosuji_Tech
takuyay0ne
1
160
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
440
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
740
WENDY [Excerpt]
tessaabrams
9
36k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
130
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.6k
From π to Pie charts
rasagy
0
130
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
470
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.6k
Done Done
chrislema
186
16k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
240
Believing is Seeing
oripsolob
1
60
Transcript
安全にNavigateしよう ゆつぼ@morayl1
自己紹介 • ゆつぼ(@morayl1) • 個人:Footprint(https://github.com/morayl/Footprint) • 仕事:2年間に及ぶ大規模Androidアプリのリニューアルを終えた • うつぼとうなぎが好き
Navigationライブラリ使ってますか? • Navigationをより安全にするinterfaceを作った話 • Fragmentの中でnavigateしている場合 • Composeの時代なので流行りの話ではない
nav_graph.xml
Navigateする方法①:idを使う • nav_graphに定義していないidを指定できてしまう • 間違えてもビルドは通る • SafeArgsが活きない • idとArgsの組み合わせを誤ると、実行時に落ちる
Navigateする方法②:Directionsを使う • 遷移アクションを間違えづらい(Directionsに生えているものしか出来ない) • Directionsを間違える可能性は残る(遷移先を選ぶときに気付ける) • Fragmentの名前が長くなると、Directionsも長くなる • SafeArgsは活きる
Directionsの仕組み nav_graphの<fragment>のidに対応したDirectionsクラスが自動生成される <fragment>内のアクションはcompanion objectに生成されている
いまいちな点まとめ • id・argsを間違える可能性 • 間違えてもビルドは通る • Directionsを間違える可能性は残る(遷移先を選ぶときに気付ける) • Fragmentの名前が長くなると、Directionsも長くなる
Navigatable Github
Navigatable Github Tには、FragmentDirections.Comanionを指定
実装側(Fragment) • navigateラムダ内でアクションが補完表示される • nav_graphに定義していない遷移が出来ない • Directionsを使っているので引数も誤らない • Directionsを空で書くのは1度だけ •
遷移時にxxDirectionsが不要なので短い!読みやすい!(個人の感想)
Navigatable
try/catch • リストなどで同時押しされて遷移する場合(FragmentA→FragmentB)、一回目のク リック処理でBに遷移した後二回目の処理時にBに遷移しようとする • が、すでにBに遷移しているため、B→Bの遷移が定義されていないと IllegalArgumentExceptionが発生する • Exceptionを用いずに、 findNavController().currentDestination?.getAction(directions.actionId)
でnullチェックすることでも回避できる A B B
まとめ Navigatableを使うと、安全・簡潔にnavigate出来ます! Navigatable.kt