티스토리 뷰
Lifecycle
LifecycleOwner
LifecycleObserver
MainActivity.kt
package com.example.lifecycleexample
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
class MainActivity : AppCompatActivity() {
private val btShow: Button by lazy {
findViewById(R.id.bt_show)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bottomLifecycleObserver = LifecycleEventObserver { source, event ->
when (source.lifecycle.currentState) {
Lifecycle.State.CREATED -> Log.e("", "MainActivity :: Lifecycle.State.CREATED")
Lifecycle.State.STARTED -> Log.e("", "MainActivity :: Lifecycle.State.STARTED")
Lifecycle.State.DESTROYED -> Log.e("", "MainActivity :: Lifecycle.State.DESTROYED")
}
when (event) {
Lifecycle.Event.ON_CREATE -> Log.e("", "MainActivity :: Lifecycle.Event.ON_CREATE")
Lifecycle.Event.ON_START -> Log.e("", "MainActivity :: Lifecycle.Event.ON_START")
Lifecycle.Event.ON_RESUME -> Log.e("", "MainActivity :: Lifecycle.Event.ON_RESUME")
Lifecycle.Event.ON_PAUSE -> Log.e("", "MainActivity :: Lifecycle.Event.ON_PAUSE")
Lifecycle.Event.ON_STOP -> Log.e("", "MainActivity :: Lifecycle.Event.ON_STOP")
Lifecycle.Event.ON_DESTROY -> Log.e("", "MainActivity :: Lifecycle.Event.ON_DESTROY")
}
}
btShow.setOnClickListener {
TestDialog().apply {
lifecycleObserver = bottomLifecycleObserver
}.show(supportFragmentManager, "TestDialog")
}
}
}
다이얼로그의 생명주기에 반응하여 로그를 출력. (LifecycleObserver)
TestDialog.kt
package com.example.lifecycleexample
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.LifecycleObserver
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
class TestDialog : BottomSheetDialogFragment() {
lateinit var lifecycleObserver: LifecycleObserver
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.e("", "TestDialog :: onCreate()")
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
viewLifecycleOwner.lifecycle.addObserver(lifecycleObserver)
Log.e("", "TestDialog :: onCreateView()")
return inflater.inflate(R.layout.dialog_test, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Log.e("", "TestDialog :: onViewCreated()")
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
Log.e("", "TestDialog :: onActivityCreated()")
}
override fun onStart() {
super.onStart()
Log.e("", "TestDialog :: onStart()")
}
override fun onResume() {
super.onResume()
Log.e("", "TestDialog :: onResume()")
}
override fun onPause() {
super.onPause()
Log.e("", "TestDialog :: onPause()")
}
override fun onStop() {
super.onStop()
Log.e("", "TestDialog :: onStop()")
}
override fun onDestroy() {
super.onDestroy()
Log.e("", "TestDialog :: onDestroy()")
}
}
MainActivity 에서 생선한 lifecycleObserver 를 다이얼로그의 생명주기에 추가한다.
viewLifecycleOwner.lifecycle.addObserver(lifecycleObserver)
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] 암호화와 복호화 (2) | 2021.06.06 |
---|---|
[Kotlin] registerForActivityResult (0) | 2021.05.27 |
[Kotlin] 위치 권한 (0) | 2021.05.27 |
[Kotlin] View (0) | 2021.05.25 |
[Kotlin] dp와 px (0) | 2021.05.25 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- XML
- DataBinding
- 혀가 길지 않은 개발자
- Intent
- CoordinatorLayout
- ArrayList
- Livedata
- ViewPager2
- View
- James Kim
- Vue.js #Vue.js + javascript
- JSONObject
- Architecture Pattern
- handler
- 안드로이드
- TabLayout
- Android
- Kotlin
- java
- coroutine
- JSONArray
- 자바
- 안드로이드 #코틀린 #Android #Kotlin
- 코틀린
- ViewModel
- activity
- MVVM
- fragment
- Design Pattern
- recyclerview
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함