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
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
660
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
330
kotlinx.datetime 使ってみた
takuji31
0
1.2k
HiltのCustom Componentについて
takuji31
0
450
java.timeをAndroidで使う
takuji31
0
220
KSPを使ってコード生成
takuji31
0
500
Kotlin Symbol Processing API (KSP) を使って Kotlin ア プリケーションの開発を効率化する
takuji31
1
3.3k
kotlinx.serialization
takuji31
0
720
kanmoba-returns-02.pdf
takuji31
0
310
Other Decks in Technology
See All in Technology
無理せず続ける、子育てエンジニアの学び
codmoninc
PRO
0
870
"優秀な誰か"の下位互換にならないために
yut0naga1_fa
1
1.8k
PHPカンファレンス関西の招待状 / Invitation to PHP Conference Kansai
ykanoh
0
120
ソフトウェア ファクトリー - 再現性のあるソフトウェアの工場づくり / Software factory
iktakahiro
6
4.9k
エンタメ企業のM&Aを支えるデータ基盤 ― 統合から店舗のリアルタイム分析まで / Data Engineering Summit 2026
genda
0
180
Railsで挑む大量データ永続化 〜600億件を永続化するための設計とボトルネックとの戦い〜
occhi
1
580
go-sqletch
moznion
0
140
LambdaWebFunctionsを試してみた / exploring-lambda-web-functions
koukihosaka
1
340
サブエージェントと Claude Mods で作る独自 Advisor ツールの話
kenichiodo
2
160
AI × アーキテクト 第1・2章|生成AIの課題とアーキテクトの素養
daidaidai
0
170
M5Stackで作る BLEマウス GPSログの世界
tomokusaba
0
420
ストレスなくAIと付き合いたい!こんなときどうしてる?
namiki64
3
530
Featured
See All Featured
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
320
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
600
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
From π to Pie charts
rasagy
1
400
The Cost Of JavaScript in 2023
addyosmani
55
10k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
460
Statistics for Hackers
jakevdp
799
230k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
630
Everyday Curiosity
cassininazir
0
350
Chasing Engaging Ingredients in Design
codingconduct
0
360
New Earth Scene 8
popppiees
4
2.7k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
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 !