'How to display lifecycles from Logcat into textview in main_fragment.xml?
I want to display real-time info from my Observer.kt class that shows the apps lifecycle ex. "On.Create", "STARTED", "OnStart", etc on my textView in my Main_fragment.xml. I have already built a Owner and Observer class:
package com.example.lifecycleaware import androidx.lifecycle.Lifecycleimport androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleRegistry class Owner: LifecycleOwner {
private val lifecycleRegistry: LifecycleRegistry
init {
lifecycleRegistry = LifecycleRegistry(this)
lifecycle.addObserver(Observer())
}
fun startOwner() {
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
}
fun stopOwner() {
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
}
override fun getLifecycle(): Lifecycle {
return lifecycleRegistry
}
}
and also:
package com.example.lifecycleaware import android.util.Log import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleObserver import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.OnLifecycleEvent class Observer: LifecycleObserver{
private val LOG_TAG = "Observer"
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onResume() {
Log.i(LOG_TAG, "onResume")
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun onPause() {
Log.i(LOG_TAG, "onPause")
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreate() {
Log.i(LOG_TAG, "onCreate")
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart() {
Log.i(LOG_TAG, "onStart")
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onStop() {
Log.i(LOG_TAG, "onStop")
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
Log.i(LOG_TAG, "onDestroy")
}
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
fun onAny(owner: LifecycleOwner, event: Lifecycle.Event) {
Log.i(LOG_TAG, owner.lifecycle.currentState.name)
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
