Bug P3
Status Update
Comments
lb...@gmail.com <lb...@gmail.com>
lb...@gmail.com <lb...@gmail.com> #2
Workaround to get things reset, as the NavController doesn't seem to have an easy way to reset itself:
When it's detected we should switch to a graph, and savedInstanceState isn't null, we finish the Activity, and start it again (recreate won't work because you get the savedInstanceState again) . I tried to investigate what keys are used in savedInstanceState, but not sure what they are, so I had to just ditch everything together... Might work for various scenarios, but sadly not for all.
supportFragmentManager.findFragmentById(R.id.navHostFragmentContentMain)!!
.findNavController().let { navController ->
val graph = try {
navController.graph
} catch (e: Exception) {
null
}
if (PermissionUtil.hasMinimalPermissionGranted(this)) {
Log.d("AppLog", "has permissions, so go to normal graph")
if (graph?.id != R.id.nav_graph) {
if (graph != null||savedInstanceState!=null) {
finish()
startActivity(Intent(this,MainActivity::class.java))
return
}
navController.setGraph(R.navigation.nav_graph)
}
} else {
Log.d("AppLog", "no permissions, so go to no-permissions graph")
if (graph?.id != R.id.nav_graph_permissions_only) {
if (graph != null||savedInstanceState!=null) {
finish()
startActivity(Intent(this,MainActivity::class.java))
return
}
navController.setGraph(R.navigation.nav_graph_permissions_only)
}
}
}
}
Description
Component used: Navigation Version used:
Devices/Android versions reproduced on: Pixel 6 with Android 14, but here I show on emulator Android 14.
What I have is 2 graphs in a simple Activity. One is used when the minimal permission isn't granted, and the other when it is.
This is in the onCreate callback of the Activity. This works fine, usually. However, for this specific scenario, it causes a crash:
The crash is:
Attached sample.