Skip to content

Instantly share code, notes, and snippets.

@ForceTower
Created June 25, 2021 00:09
Show Gist options
  • Save ForceTower/ff967486704e8415e18e43ccc5a8d91c to your computer and use it in GitHub Desktop.
Save ForceTower/ff967486704e8415e18e43ccc5a8d91c to your computer and use it in GitHub Desktop.
Navigation deep link emulation
fun NavDestination.buildExplicitly(): IntArray {
val hierarchy = ArrayDeque<NavDestination>()
var current: NavDestination? = this
do {
val parent = current!!.parent
if (parent == null || parent.startDestination != current.id) {
hierarchy.addFirst(current)
}
current = parent
} while (current != null)
val deepLinkIds = IntArray(hierarchy.size)
var index = 0
for (destination in hierarchy) {
deepLinkIds[index++] = destination.id
}
return deepLinkIds
}
val graph = NavInflater(this, PermissiveNavigatorProvider()).inflate(R.navigation.home_graph)
val node = graph.findNode(R.id.home)
val res = node!!.buildExplicitly()
intent.putExtra("android-support-nav:controller:deepLinkIds", res)
// res is graph, backstack ids, destination
// res is intArrayOf(R.id.home_graph_xml, R.id.home)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment