我相信上述解决方案应该有效。但我的问题不同。有一个第三方 sdk 使用我提供的上下文启动其活动,并将结果传递给我必须实现的侦听器。
所以我没有选择合作onActivityResult
:(
我用下面的黑客来解决这个问题:
private var runnable: Runnable? = null // Runnable object to contain the navigation code
override fun onResume() {
super.onResume()
// run any task waiting for this fragment to be resumed
runnable?.run()
}
override fun responseListener(response: Response) { // Function in which you are getting response
if (!isResumed) {
// add navigation to runnable as fragment is not resumed
runnable = Runnable {
navController.navigate(R.id.destination_to_navigate)
}
} else {
// navigate normally as fragment is already resumed
navController.navigate(R.id.destination_to_navigate)
}
}
让我知道是否有更好的解决方案。目前我发现这非常简单且易于实现:)