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
170
CameraX使ってみた
takuji31
0
310
kotlinx.datetime 使ってみた
takuji31
0
1.1k
HiltのCustom Componentについて
takuji31
0
390
java.timeをAndroidで使う
takuji31
0
210
KSPを使ってコード生成
takuji31
0
490
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
「面白い!」を信じ抜け。激動の時代を貫く、オンリーワン・エンジニアの条件
kizawa2020
0
190
Windows の互換機能 - 古いプログラムはなぜ動作できるのか
murachiakira
PRO
0
100
AI駆動開発はどこまで来たのか? ファインディの最新実態調査で読み解く現在地 Devin Con Tokyo
akiratom
1
720
フィジカルAIの知識ゼロの僕でも AIを使えばここまでできた
tatsuya1970
0
170
AIにブラウザを触らせて、E2EテストをPlaywrightで書く
koji_kawamura
0
410
dbt と Snowflake と tag
kevinrobot34
1
230
英語が話せなくてもKubeConスタッフに参加した話
yusuke427
2
1.1k
Contract One Engineering Unit 紹介資料
sansan33
PRO
0
20k
RelayerというPHPのフレームワークを作った
polidog
PRO
0
140
[ホンマでっか SRE] あなたはなぜ SRE に?
_awache
0
130
AI時代の「OAuth認証」にどう物申すか?(OAuth/OIDC Numa (Immersion) Workshop 2026)
oidfj
PRO
0
200
The Django UUID Story - DjangoCon US 2026
pauloxnet
0
250
Featured
See All Featured
A Soul's Torment
seathinner
6
3.5k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
800
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
470
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Automating Front-end Workflow
addyosmani
1369
210k
First, design no harm
axbom
PRO
2
1.3k
GitHub's CSS Performance
jonrohan
1033
470k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.3k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
510
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
290
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 !