startActivityForResult() between Dynamic Feature Modules with Android Architecture Navigation

105 views Asked by At

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.

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(). 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()

How can we call startActivityForResult() using the navigation direction defined in the Android Architecture Navigation?

1

There are 1 answers

0
Sebas LG On

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)
  2. Get the action ID and use it to get the Action from the navigation controller
  3. Get the destination ID from the Action
  4. Find the node in the graph using the destination ID
  5. Get the Intent from that ActivityNavigator.Destination node
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()