I'm using the Navigation Architecture Component and I have a setup similar to this one for popping the stack when navigating to a particular fragment:
<action
android:id="@+id/navigate_to_main_screen"
app:destination="@id/fragment_main_screen"
app:popUpTo="@+id/navigation_main"
app:popUpToInclusive="true"/>
This works almost as expected. Both the system back button and the up icon in the app bar don't navigate to the previous fragment. The system back button exits the app.
However, the up button in the app bar is still there, clicking it doesn't do anything as expected. What am I doing wrong? Why is this still here?
In the main activity I already have
AppBarConfiguration config =
new AppBarConfiguration.Builder(navController.getGraph()).build();
NavigationUI.setupActionBarWithNavController(this, navController, config);
and
@Override
public boolean onSupportNavigateUp() {
return navController.navigateUp() || super.onSupportNavigateUp();
}
As per the documentation.
The library version I'm using:
implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha09'
implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha09'
The navigation component makes it easier by removing the redundant code for linking the items to destinations. For creating an options menu, follow these steps: Step 1. Create the destination fragment The destination fragment must be in the nav graph for this to work. So if its not there add it or create a new destination.
A pop-up button displays a menu of mutually exclusive options. After people choose an item from a pop-up button’s menu, the menu closes, and the button can update its content to indicate the current selection.
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.
It is the primary collection of menu items for an activity which may include actions like Search, Settings, and About us. Generally, a menu file is created and each of its items is linked to their respective destinations progmatically. The navigation component makes it easier by removing the redundant code for linking the items to destinations.
If you want to customize which destinations are considered top-level destinations, you can instead pass a set of destination IDs to the constructor, as shown below.
To solve your problem, replace
AppBarConfiguration config =
new AppBarConfiguration.Builder(navController.getGraph()).build();
With
AppBarConfiguration config =
new AppBarConfiguration.Builder(R.id.navigation_main, R.id.fragment_main_screen).build();
More details here: AppBarConfiguration
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With