Created
December 19, 2018 18:18
-
-
Save tiwiz/72f0129a2de2b66695daaf54b1ffc2f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.arch.lifecycle.Lifecycle | |
import android.arch.lifecycle.LifecycleObserver | |
import android.arch.lifecycle.LifecycleOwner | |
import android.arch.lifecycle.OnLifecycleEvent | |
import com.nytimes.android.logger.Logger | |
class LifecycleLogger private constructor(private val className: String, | |
private val lifecycle: Lifecycle, | |
private val tag: String) : LifecycleObserver { | |
companion object { | |
fun attachTo(lifecycleOwner: LifecycleOwner, tag: String = "") { | |
lifecycleOwner.lifecycle.addObserver( | |
LifecycleLogger(lifecycleOwner.javaClass.simpleName, lifecycleOwner.lifecycle, tag) | |
) | |
} | |
} | |
private fun logMethod(methodName: String) { | |
Logger.d("[$className] $tag - $methodName") | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_ANY) | |
fun onAnyEvent() { | |
logMethod(lifecycle.currentState.name) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment