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
Dagger2 and Android Lifecycle with Kotlin
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Takuji Nishibayashi
April 26, 2016
Technology
650
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Dagger2 and Android Lifecycle with Kotlin
Takuji Nishibayashi
April 26, 2016
More Decks by Takuji Nishibayashi
See All by Takuji Nishibayashi
compose-hot-reload を試そうとした話
takuji31
0
160
CameraX使ってみた
takuji31
0
310
kotlinx.datetime 使ってみた
takuji31
0
1.1k
HiltのCustom Componentについて
takuji31
0
380
java.timeをAndroidで使う
takuji31
0
210
KSPを使ってコード生成
takuji31
0
480
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
3.2k
kotlinx.serialization
takuji31
0
700
kanmoba-returns-02.pdf
takuji31
0
300
Other Decks in Technology
See All in Technology
探索・可視化・自動化を一本化 Amazon Quickでデータ活用スピードを上げる方法
koheiyoshikawa
0
210
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
15
110k
データベース研修【MIXI 26新卒技術研修】
mixi_engineers
PRO
1
330
インシデント事例と パッケージの全量解析に学ぶ ソフトウェアサプライチェーンの守り方 / supply-chain-attack-defense
flatt_security
0
1.1k
人とエージェントが高め合う協業設計
kintotechdev
0
950
2026年のソフトウェア開発を考える(2026/07版) / Agentic Software Engineering 2026-07 Findy Edition
twada
PRO
29
16k
コンテナ・K8s研修【MIXI 26新卒技術研修】
mixi_engineers
PRO
1
260
AIとハーネスで育てるトランスコンパイラ / 20260722 Yasushi Katayama
shift_evolve
PRO
3
970
WEBフロントエンド研修【MIXI 26新卒技術研修】
mixi_engineers
PRO
1
320
『モデル + ハーネス』で読み解く AIエージェント入門
oracle4engineer
PRO
2
200
AIが当たり前の組織で エンジニアはどう育つか
nishihira
1
1.3k
副作用のある Lambda でも Lambda Power Tuning は使えるのか / lambda-power-tuning-side-effects
koukihosaka
2
150
Featured
See All Featured
Designing for Performance
lara
611
70k
Navigating Team Friction
lara
192
16k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.5k
Information Architects: The Missing Link in Design Systems
soysaucechin
0
1k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.4k
We Have a Design System, Now What?
morganepeng
55
8.2k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
600
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.1k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.7k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
320
Transcript
Dagger2 and Android Lifecycle with Kotlin @takuji31
@takuji31 (Takuji Nishibayashi)
Loves Kotlin
Dagger2
de facto standard of DI container for Android (may be)
Compile )me dependency resolu)on
Scoped singleton
Example of use
Inject context
Inject presenter
Inject Realm
Two Headaches for Android of the life cycle with injected
instances
Headaches • Handle lifecycle events • Retain some instance during
configura7on change
Handle lifecycle events
Stop UI refreshing in presenter
Cancel API request
Close Realm instance
Unsubscribe RxJava subscrip1on
Retain some instance during screen rota0on
Keep API request
Loaded model instances
But, Loader is difficult !
Solu%on
Headaches • Handle lifecycle events ! • Retain some instance
during configura7on change
Create lifecycle provider
Lifecycle provider interface ActivityLifecycle { fun onCreate(savedInstanceStete: Bundle?) {} fun
onSaveInstanceState(outState: Bundle?) {} fun onDestroy() {} //... }
Lifecycle provider object Lifecycles { fun activity(vararg lifecycles: ActivityLifecycle): ActivityLifecycle
{ return object : ActivityLifecycle { override fun onCreate(savedInstanceStete: Bundle?) { lifecycles.forEach { it.onCreate(savedInstanceStete = savedInstanceStete) } } override fun onStart() { lifecycles.forEach { it.onStart() } } //... } }
Provide Lifecycle provider @Module class UserModule() { @Provides @PerActivity fun
lifecycle(presenter: UserPresenter) : ActivityLifecycle { return presenter //or return Lifecycles.activity(presenter, some, lifecycle, objects) } }
Use lifecycle provider class UserActivity : AppCompatActivity() { @Inject lateinit
var lifecycle : ActivityLifecycle var component : UserComponent by Delegates.notNull() override fun onCreate(savedInstanceState: Bundle?) { lifecycle.onCreate(savedInstanceStete = savedInstanceState) } override fun onDestroy() { lifecycle.onDestroy() } }
Headaches • Handle lifecycle events • Retain some instance during
configura7on change !
Use retain fragment for keep component
Retain fragment class UserComponentFragment : Fragment() { var component: UserComponent?
= null init { retainInstance = true } companion object { val TAG = "UserComponentFragment" } }
Use retain fragment class UserActivity : AppCompatActivity() { override fun
onCreate(savedInstanceState: Bundle?) { var storeFragment = supportFragmentManager .findFragmentByTag(UserComponentFragment.TAG) as? UserComponentFragment if (storeFragment == null) { storeFragment = UserComponentFragment() supportFragmentManager .beginTransaction() .add(storeFragment, UserComponentFragment.TAG) .commit() } if (storeFragment.component == null) { //init component } storeFragment.component.inject(this) } }
see. takuji31/Dagger2Android
Enjoy Dagger2 and Android life !