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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
320
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
710
kanmoba-returns-02.pdf
takuji31
0
300
Other Decks in Technology
See All in Technology
書籍『生成AIの安全性入門』の入門
wataoka
0
210
プロダクト思考 × 基盤思考を AIで実現する Compound Engineering
tkc66buzz
1
240
Bet AI Day 2026丨How We Bet AI: AIとともに働く場をつくる
layerx
PRO
1
2.1k
AI時代に、プロダクトの数だけ積み上がる所有コストをどうエンジニアリングするか / Engineering the Cost of Ownership
kzkmaeda
0
940
AIエージェントを雇う前に決める5つのこと
knishioka
1
150
Azure Cost Management の FOCUS コストデータを迷わず読むための“3つの軸”
tetsuyaooooo
0
140
現場に行くだけでは足りない——プロダクトエンジニアが業務の流れを捉える観点と、その鍛え方
takumiengineering
0
230
はじめてのDatabricks:技術者向けワークショップ / beginner-workshop
databricksjapan
PRO
0
140
Bet AI Day 2026丨Agentは、「金融」という巨大産業の何を変えられるのか
layerx
PRO
0
710
作り直せるコードは迅速に 作り直せないDBは慎重に - AI時代のプロダクトエンジニアが「判断の不可逆性」で開発速度を変える話
kinosuke01
0
190
AI-DLCって実際どう? 〜聞きたいこと全部聞いてみる〜
news_it_enj
0
220
DGX Sparkを2台使って いろいろ動かす話
sonoda_mj
1
120
Featured
See All Featured
The browser strikes back
jonoalderson
0
1.6k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.5k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
320
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
410
The untapped power of vector embeddings
frankvandijk
2
1.9k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.6k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
560
Fireside Chat
paigeccino
42
4k
Documentation Writing (for coders)
carmenintech
77
5.5k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
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 !