Search code examples
javaandroidnavigationandroid-architecture-navigationappbar

NavigationUI shows back (up) button on Home Destination


I am trying to implement NavigationUI with Action Bar. I cannot get rid of the back button on my startDestination.

I tried getActionBar().setDisplayShowHomeEnabled(false); and getActionBar().setHomeButtonEnabled(false); however getActionBar() returns null. I am new to Android programming and I couldn't find a solution. Any help is appreciated. Thanks.

Main Activity:

public class MainActivity extends AppCompatActivity {

    NavController navController;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        navController = Navigation.findNavController(this, R.id.my_nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this,  navController);
    }


    @Override
    public boolean onSupportNavigateUp() {
        navController.navigateUp();
        return super.onSupportNavigateUp();
    }
}

Main Activity Layout:

<fragment
    android:id="@+id/my_nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/app_navigation" />

NavGraph with the startDestination:

<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/app_navigation.xml"
    app:startDestination="@id/mainFragment">
    <fragment
        android:id="@+id/mainFragment"
        android:name="com.tarpinyan.navigationui_test.MainFragment"
        android:label="MainFragment" >
        <action
            android:id="@+id/action_mainFragment_to_timerFragment"
            app:destination="@id/timerFragment" />
        <action
            android:id="@+id/action_mainFragment_to_settingFragment"
            app:destination="@id/settingFragment" />
        <action
            android:id="@+id/action_mainFragment_to_addFragment"
            app:destination="@id/addFragment" />
    </fragment>
    <fragment
        android:id="@+id/settingFragment"
        android:name="com.tarpinyan.navigationui_test.SettingFragment"
        android:label="SettingFragment" />
    <fragment
        android:id="@+id/addFragment"
        android:name="com.tarpinyan.navigationui_test.AddFragment"
        android:label="AddFragment" />
    <fragment
        android:id="@+id/timerFragment"
        android:name="com.tarpinyan.navigationui_test.TimerFragment"
        android:label="TimerFragment" />
</navigation>

Main Fragment:

public class MainFragment extends Fragment {

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        setHasOptionsMenu(true);

        
        return view;
    }


    @Override
    public void onCreateOptionsMenu(@NonNull @NotNull Menu menu, @NonNull @NotNull MenuInflater inflater) {
        inflater.inflate(R.menu.main_menu, menu);
    }


    @Override
    public boolean onOptionsItemSelected(@NonNull @NotNull MenuItem item) {
        if (item.getItemId() == R.id.menu_settings) {
            NavHostFragment.findNavController(this).navigate(R.id.action_mainFragment_to_settingFragment);
        }
        return true;
    }
}

Main Fragment with the back button


Solution

  • To remove the UP button from the first fragment, you need to use the 3-arg version of setupActionBarWithNavController() method that takes a AppBarConfiguration in which you can specify fragments that you want to remove the UP button from.

    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.mainFragment) // Remove Up button from mainFragment 
                .build();
    
    setupActionBarWithNavController(this,  navController, appBarConfiguration);
    

    Update:

    the result is the same, I implemented the 3 arg version but still Main Fragment appears with the up button

    Sounds like you explicitly enabled showing the UP button the the main fragment. You need to make sure that setDisplayHomeAsUpEnabled(true) is not enabled in your mainFragment.

    Or you can explicitly disable it by:

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        ((AppCompatActivity)requireActivity()).getSupportActionBar()
                           .setDisplayHomeAsUpEnabled(false); // Hide the UP button
    
        // ....
    }