Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Component's popUpTo not removing up button

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'
like image 789
Danish Khan Avatar asked Jan 02 '19 09:01

Danish Khan


People also ask

How to create an options menu using the navigation component?

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.

What is a pop-up button?

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.

What is popupto in react app?

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.

What is a navigation component?

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.


1 Answers

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

like image 143
Alexey Denysenko Avatar answered Oct 26 '22 07:10

Alexey Denysenko