Jan
05/24/2023, 8:14 AMclass SomeComponent(
componentContext: ComponentContext
) : ComponentContext by componentContext {
init {
lifecycle.subscribe(
object : Lifecycle.Callbacks {
override fun onCreate() {
}
}
)
}
}
class SomeComponent(
componentContext: ComponentContext
) : ComponentContext by componentContext {
private val lifeCycleObserver: Lifecycle.Callbacks = object : Lifecycle.Callbacks {
// listen to onCreate, onStart, onPause, onResume, onStop, onDestroy
}
init {
lifecycle.doOnCreate {
lifecycle.subscribe(lifeCycleObserver)
}
lifecycle.doOnDestroy {
lifecycle.unsubscribe(lifeCycleObserver)
}
}
}
Arkadii Ivanov
05/24/2023, 8:48 AMlifecycle.doOnCreate {}
instead of the anonymous class.Jan
05/24/2023, 8:54 AMArkadii Ivanov
05/24/2023, 8:57 AMlifecycle.subscribe(this)
.Jan
05/24/2023, 8:57 AMabstract class AComponent(
componentContext: ComponentContext
) : Lifecycle.Callbacks, ComponentContext by componentContext {
init {
lifecycle.subscribe(this)
}
override fun onCreate() {
}
override fun onDestroy() {
}
override fun onPause() {
}
override fun onResume() {
}
override fun onStart() {
}
override fun onStop() {
}
}
Arkadii Ivanov
05/24/2023, 9:03 AMinit
section is called.Lifecycle.Callbacks
interface where needed and override only required methods, and just call lifecycle.subscribe(this)
as the last statement in the init
section.Jan
05/24/2023, 9:07 AMArkadii Ivanov
05/24/2023, 9:08 AMJan
05/24/2023, 9:12 AMArkadii Ivanov
05/24/2023, 9:25 AMlifecycle.doOnStart { onTitleChangeAction("some title") }
.Jan
05/24/2023, 9:30 AM