6

I have an app module with main navigation graph and feature module with it's own navigation graph. Feature navigation is included to main navigation graph. So feature fragments and navigation graph knows nothing about app module navigation graph. I need to pop back stack from feature fragment A to fragment B in main graph. What's the best way to achieve this?

Wishmaster
  • 850
  • 10
  • 19
  • https://stackoverflow.com/questions/52736412/how-to-navigate-from-nested-fragment-to-parent-fragment-using-jetpack-navigation This thread saved my day! – Taslim Hartmann Oct 05 '19 at 12:58

2 Answers2

2

refer to https://developer.android.com/guide/navigation/navigation-design-graph

You could popup from Fragment A to startDestination Fragment in app module by using global action.

In your feature module nav graph, add:

<!-- Action back to destination which launched into this in_game_nav_graph-->
      <action android:id="@+id/action_pop_out_of_game"
              app:popUpTo="@id/using_your_app_nav_graph_id_here"
              app:popUpToInclusive="true" />

Then you could navigate to Fragment B from the startDestination defined in your app nav graph.

pluto
  • 390
  • 2
  • 10
0

In the Activity with your NavHostFragment just pop back the stack to the fragment you want to return to. This will only work of course , if you use the same Activity to manage the main and feature fragment.

NavController controller = Navigation.findNavController(this, R.id.nav_host_fragment);
controller.popBackStack(R.id.the_id_of_fragment_defined_in_nav_graph, false);

Assuming the navigation graph code looks like this:

<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/mainMenuFragment">
    <fragment
    android:id="@+id/mainMenuFragment"
    android:name="com.package.MainMenuFragment"
    android:label="MainMenuFragment"
    tools:layout="@layout/main_menu_fragment">
    <action
        android:id="@+id/open_feature"
        app:destination="@id/feature_graph" />
    </fragment>
    <include app:graph="@navigation/feature_graph" />
</navigation>
Martin Vichev
  • 69
  • 1
  • 4