问题
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'
回答1:
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
来源:https://stackoverflow.com/questions/54003666/navigation-components-popupto-not-removing-up-button