简体   繁体   English

具有 Android 架构导航的动态功能模块之间的 startActivityForResult()

[英]startActivityForResult() between Dynamic Feature Modules with Android Architecture Navigation

We have a graph A with activity A1 and we want to open graph B with activity B1 using startActivityForResult() to get a result back from B1 to A1.我们有一个带有活动 A1 的图 A,我们想使用startActivityForResult()打开带有活动 B1 的图 B,以将结果从 B1 返回到 A1。

The problem is that A and B graphs live in separate dynamic feature modules so in A1 we don't have access to the class of activity B1 to call startActivityForResult() .问题是 A 和 B 图存在于单独的动态功能模块中,因此在 A1 中我们无法访问活动类 B1 来调用startActivityForResult() We do have the navigation graphs of both modules connected so that we can navigate between modules but Android Architecture Navigation does not support startActivityForResult()我们确实连接了两个模块的导航图,以便我们可以在模块之间导航,但 Android 架构导航不支持startActivityForResult()

How can we call startActivityForResult() using the navigation direction defined in the Android Architecture Navigation?我们如何使用 Android 架构导航中定义的导航方向调用startActivityForResult()

It is possible to get the launch intent of an action defined in the navigation graph.可以获取导航图中定义的操作的启动意图。 To achieve it you need to:要实现它,您需要:

  1. Get the navigation direction (NavDirections)获取导航方向 (NavDirections)
  2. Get the action ID and use it to get the Action from the navigation controller获取操作 ID 并使用它从导航控制器获取操作
  3. Get the destination ID from the Action从 Action 中获取目标 ID
  4. Find the node in the graph using the destination ID使用目标 ID 在图中查找节点
  5. Get the Intent from that ActivityNavigator.Destination node从该 ActivityNavigator.Destination 节点获取 Intent
val direction = MyGeneratedDirections.actionOpenMyDestination(myParam)
val destId = findNavController().currentDestination.getAction(direction.actionId).destinationId
val intent = (findNavController().graph.findNode(destId) as? ActivityNavigator.Destination)?.intent

See https://developer.android.com/reference/androidx/navigation/ActivityNavigator.Destination#getIntent()请参阅https://developer.android.com/reference/androidx/navigation/ActivityNavigator.Destination#getIntent()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM