2

I have an issue using Navigation Controller. I have 4 fragments which are Main Fragments, Add Fragments, List Fragments & Details Fragments.

My Main fragment can navigate to both Add & List. While My Add fragments only can navigate to List after adding some data.

I enter my fragment from Main -> Add -> List. How can I navigate my fragment to Main from List after I added some data? I want my direction to look like Main <- List without going to Add.

I'm using Navigation.findNavController(getActivity(), R.id.nav_host_fragment).navigateUp(); as a back direction. I'm pretty new to Navigation Controller

Haziq Abdullah
  • 83
  • 1
  • 10

1 Answers1

0

You should be able to achieve this by using the popUpTo / popUpToInclusive attributes. As per the documentation, it states:

When navigating using an action, you can optionally pop additional destinations off of >the back stack. For example, if your app has an initial login flow, once a user has >logged in, you should pop all of the login-related destinations off of the back stack so >that the Back button doesn't take users back into the login flow.

To pop destinations when navigating from one destination to another, add an app:popUpTo >attribute to the associated element. app:popUpTo tells the Navigation library >to pop some destinations off of the back stack as part of the call to navigate(). The >attribute value is the ID of the most recent destination that should remain on the >stack.

You can also include app:popUpToInclusive="true" to indicate that the destination >specified in app:popUpTo should also be removed from the back stack.

Community
  • 1
  • 1
anaebede
  • 11
  • 4