Because fragments are not used in Jetpack navigation, I want to observe the lifecycle of jetpack navigator to get current destination or screen name. As you know Jetpack navigation is not using fragments rather it uses deep linking structure. I searched a lot but couldn't find any suitable solution.
I want to get the route or destination name for my sdk to track the screens flow.
class AppLifeCycleObserver : Application.ActivityLifecycleCallbacks {
var route: String? = ""
override fun onActivityCreated(p0: Activity, p1: Bundle?) {
log("onActivityCreated")
}
val TAG = "AppLifeCycleObserver"
fun log(message: String) {
Log.e(TAG, message)
}
override fun onActivityStarted(p0: Activity) {
log("onActivityStarted")
if (p0 is FragmentActivity) {
p0.supportFragmentManager.registerFragmentLifecycleCallbacks(object :
FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentCreated(
fm: FragmentManager,
f: Fragment,
savedInstanceState: Bundle?
) {
super.onFragmentCreated(fm, f, savedInstanceState)
log("onFragmentCreated")
}
override fun onFragmentDestroyed(fm: FragmentManager, f: Fragment) {
super.onFragmentDestroyed(fm, f)
log("onFragmentDestroyed")
}
}, true)
} else if (p0 is ComponentActivity) {
(p0 as MasterActivity).navController_field.currentBackStackEntry.destination.route
log("ComponentActivity")
}
}
override fun onActivityResumed(p0: Activity) {
log("onActivityResumed")
}
override fun onActivityPaused(p0: Activity) {
log("onActivityPaused")
}
override fun onActivityStopped(p0: Activity) {
log("onActivityStopped")
}
override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {
}
override fun onActivityDestroyed(p0: Activity) {
log("onActivityDestroyed")
}
}
I can't access the
(p0 as MasterActivity).navController_field.currentBackStackEntry.destination.route
because this is inaccessible , I find it through debugging