我正在尝试将一个应用程序移植到GoogleIO'18发布的新导航体系结构组件上。
假设我需要使用通常以startActivityForResult启动的活动。这个活动要么来自库,要么来自系统活动,所以我不能修改它。
是否有任何方法将此活动作为目标包含在导航图中并从中获得结果?
发布于 2018-05-29 13:34:14
到目前为止,我唯一的解决方案是将该活动封装在一个片段后面,该片段捕获结果,然后将其呈现给导航图:
class ScannerWrapperFragment : Fragment() {
private val navController by lazy { NavHostFragment.findNavController(this) }
override fun onResume() {
super.onResume()
// forward the call to ScannerActivity
// do it in onResume to prevent call duplication from configuration changes
val intent = Intent(context, ScannerActivity::class.java)
startActivityForResult(intent, 4304357)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == 4304357) {
if (resultCode == RESULT_OK) {
val params = Bundle().apply {
putString("scan_result", data?.extras?.getString("scan_result"))
}
//present the scan results to the navigation graph
navController.navigate(R.id.action_scanner_to_result_screen, params)
} else {
navController.popBackStack()
}
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}
}https://stackoverflow.com/questions/50488351
复制相似问题