Skip to content

Instantly share code, notes, and snippets.

@fdoyle
Created June 23, 2022 16:41
Show Gist options
  • Save fdoyle/5ed52cb41bd6ca9978d25abd38aa3d47 to your computer and use it in GitHub Desktop.
Save fdoyle/5ed52cb41bd6ca9978d25abd38aa3d47 to your computer and use it in GitHub Desktop.
//from your application class, call
// new NavigationLogger().start(this);
//logs activity and fragment lifecycle events.
class NavigationLogger {
val logger = PVLog("Navigation")
fun start(app: Application) {
app.registerActivityLifecycleCallbacks(object: Application.ActivityLifecycleCallbacks {
override fun onActivityCreated(p0: Activity, p1: Bundle?) {
logger.log("Activity Created: ${p0.javaClass.simpleName}")
if(p0 is FragmentActivity) {
p0.supportFragmentManager.registerFragmentLifecycleCallbacks(object: FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentStarted(fm: FragmentManager, f: Fragment) {
logger.log("Fragment Started: ${f.javaClass.simpleName}")
}
override fun onFragmentAttached(fm: FragmentManager, f: Fragment, context: Context) {
logger.log("Fragment Attached: ${f.javaClass.simpleName} to ${context.javaClass.simpleName}")
}
}, true)
}
}
override fun onActivityStarted(p0: Activity) {
logger.log("Activity Started: ${p0.javaClass.simpleName}")
}
override fun onActivityResumed(p0: Activity) {
logger.log("Activity Resumed: ${p0.javaClass.simpleName}")
}
override fun onActivityPaused(p0: Activity) {
logger.log("Activity Paused: ${p0.javaClass.simpleName}")
}
override fun onActivityStopped(p0: Activity) {
logger.log("Activity Stopped: ${p0.javaClass.simpleName}")
}
override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {
//do nothing
}
override fun onActivityDestroyed(p0: Activity) {
logger.log("Activity Destroyed: ${p0.javaClass.simpleName}")
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment