Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
"おまじない"を卒業する ボイラープレート再入門
Search
shun
April 22, 2026
Technology
310
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
"おまじない"を卒業する ボイラープレート再入門
shun
April 22, 2026
Other Decks in Technology
See All in Technology
TinyGoでオブザーバビリティ?OpenTelemetryはTinyGoで使えるのか / Is OpenTelemetry available in TinyGo environment?
ymotongpoo
2
140
Running Kotlin + Quarkus with Lambda
nealle
1
4k
Metadata - Section 1: HTML Basics
mohab1
0
120
大型犬好きによるPugの紹介
yamanoku
1
160
PHPカンファレンス関西の招待状 / Invitation to PHP Conference Kansai
ykanoh
0
210
「Docker Desktop不要の時代」は 来たのか?
ueponx
0
830
サブエージェントと Claude Mods で作る独自 Advisor ツールの話
kenichiodo
2
170
パッチ更新でterraform initが落ちた日
grimoh
1
140
Rust で関数型ドメインモデリング
takonda
0
170
ブラウザとサーバーから考えるAI時代のフロントエンド監視
shogo4131
3
1k
会社紹介資料 / Sansan Company Profile
sansan33
PRO
31
430k
Temporal 最高!でも改めて「日付」について調べたら人類の負債すぎる件
riyaamemiya
0
180
Featured
See All Featured
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
600
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.4k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.4k
Believing is Seeing
oripsolob
1
250
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.1k
Heart Work Chapter 1 - Part 1
lfama
PRO
10
37k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
520
Side Projects
sachag
456
43k
Discover your Explorer Soul
emna__ayadi
2
1.3k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
460
Automating Front-end Workflow
addyosmani
1369
210k
Transcript
“おまじない”を卒業する ボイラープレート再入門 2026/04/21 Kotlin愛好会 vol. 64@ウェルスナビ株式会社
自己紹介 Shun 27卒 X: @shunsuke_1B Androidアプリ開発が好き #Android(Kotlin)一年生 #Jetpackネイティブ世代 #iOS(Swift)もちょこっとだけ
このコードの違い 説明できますか? はじめに
本日のゴール collectAsState と collectAsStateWithLifecycle の違いを知ることでおまじないを一つ卒業する
目次 1. collectAsStateと‥WithLifecycleの違い 2. collectAsStateWithLifecycleとは 3. ‥ WithLifecycleはどうライフサイクルを考慮しているのか 4. KMP/CMP環境でのiOSのライフサイクルとの対応
5. 実際に検証してみる 6. まとめ
共通 • StateFlow/Flow から流れてくる値を collect し て State に書き込む •
Composable が Composition から外れると collect を停止する collectAsState • Android のライフサイクルを考慮しない collectAsStateWithLifecycle • Android のライフサイクルを考慮する • アプリがバックグラウンド状態の時にcollect を停止する collectAsStateと‥WithLifecycleの違い 引用:https://medium.com/androiddevelopers/consuming- flows-safely-in-jetpack-compose-cde014d0d5a3
引用:https://developer.android.com/jetpack/androidx/releases/lifecycle collectAsStateWithLifecycleとは
collectAsStateWithLifecycle 登場前、ライフサイクルを考慮して collect を制御したい場合はこのよう なボイラープレートコードを毎回記述していました collectAsStateWithLifecycleとは
collectAsStateWithLifecycle の登場により、このようにすっきり書けるようになりました collectAsStateWithLifecycleとは
minActiveState=STARTED がデフォルト引数として設定されています。 その値を repeatOnLifecycle に渡し、collect の開始/停止を制御しています。 …WithLifecycleはどうライフサイクルを考慮しているのか
先ほど渡した state(minActiveState) を upTo/downFrom に渡し、collect を開始/停止するための イベントを startWorkEvent/cancelWorkEvent として受け取っています。 upTo/downFromのソースコード
…WithLifecycleはどうライフサイクルを考慮しているのか
引用 :AndroidCodelab startWorkEvent = ON_START event(ライフサイクルイベント) == ON_START …WithLifecycleはどうライフサイクルを考慮しているのか
引用 :AndroidCodelab ON_STARTでStartedに入るとcollectを開始する …WithLifecycleはどうライフサイクルを考慮しているのか
引用 :AndroidCodelab cancelWorkEvent = ON_STOP event(ライフサイクルイベント) == ON_STOP …WithLifecycleはどうライフサイクルを考慮しているのか
引用 :AndroidCodelab ON_STOP で Started を出ると collect を停止する …WithLifecycleはどうライフサイクルを考慮しているのか
引用 :AndroidCodelab つまり、minActiveState に指定した状態に入るイベント で collect を開始し、その状態から出るイベントでcollect を停止するようになっています。 …WithLifecycleはどうライフサイクルを考慮しているのか
minActiveStateをResumedにすると ON_RESUME でcollect開始 ON_PAUSE でcollect停止となり Resumed の間だけcollectするようにできます 引用 :AndroidCodelab …WithLifecycleはどうライフサイクルを考慮しているのか
引用 :AndroidCodelab upTo/downFromのソースコード
現在 Androidx.lifecycle など多くのJetpackライブラリがKMP対応となっています 引用:https://kotlinlang.org/docs/multiplatform/compose- lifecycle.html#mapping-android-lifecycle-to-other-platforms KMP/CMP環境でのiOSのライフサイクルとの対応
iOS のネイティブイベントを Android のライフサイクルイベントにマッピングしています。 そのため collectAsStateWithLifecycle は iOS でもそのまま利用可能です。 引用:https://kotlinlang.org/docs/multiplatform/compose-
lifecycle.html#mapping-android-lifecycle-to-other-platforms KMP/CMP環境でのiOSのライフサイクルとの対応
フルComposeの場合 VCイベント(赤枠) はアプリ起動時の viewWillAppear 以外、発火しません。 主に動作するのは アプリ状態(青枠) のイベントです。 KMP/CMP環境でのiOSのライフサイクルとの対応 引用:https://kotlinlang.org/docs/multiplatform/compose-
lifecycle.html#mapping-android-lifecycle-to-other-platforms
実際に検証してみる
これらを踏まえて collectAsState と collectAsStateWithLifecycle それぞれどのようなユースケースで使 用するべきか collectAsState • マルチプラットフォームでの開発(?) collectAsStateWithLifecycle
• それ以外全て 原則 collectAsStateWithLifecycle を使うべきと考えました。 バックグラウンド時も collect を続けることは、不要な Recomposition の発生や、通信・位置情報の更 新を維持することになるため、CPU・メモリ・データ通信量を無駄に消費します。 collectAsStateWithLifecycle なら stateIn(WhileSubscribed(5000)) と組み合わせることで collectが停止し た5秒後に上流の流れまで止めることが可能です。 まとめ
ご清聴ありがとうございました 今回使用した検証用アプリのリポジトリです