Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottom navigation: NavigationUI.setupActionBarWithNavController() not working for fragment exchanging

Background: (can be ignored if you don't wanna know how I got the problem. Just some question about Android Studio template activity)

I have been working on a bottom navigation and tried to copy the code of MainActivity.java from Android Studio defaultly created template of Bottom Navigation Activity.

Choosing template

Also I've found the documentation of this UI from Android Studio. Bottom Navigation
Basically, the main code in MainActivity should be like this for calling interaction of bottom navigation:

    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
            R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
    NavigationUI.setupWithNavController(navView, navController);


My Problem:
My xml of bottom navigation works perfect. But I just found the NavigationUI.setupActionBarWithNavController() and NavigationUI.setupWithNavController() are not working. My app just flashed and stopped whenever I run it. Kinda weird but I believe these two methods are the official usage of bottom navigation.

HomePage.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#267A9E"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>


<fragment
    android:id="@+id/frag_container"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="612dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toBottomOf="@id/bottom_navi"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/nav_graph" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_navi"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/bottom_navigation" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="53dp"
    android:layout_height="56dp"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:backgroundTint="#4a90c9"
    app:layout_constraintBottom_toTopOf="@+id/bottom_navi"
    app:layout_constraintEnd_toEndOf="parent"
    app:srcCompat="@drawable/plus" />


</androidx.constraintlayout.widget.ConstraintLayout>




SOLUTION:
Now i'm using a traditional way (set click listener) to implement this navigation properly. It's old fashion but very flexible as I have my personized toolbar and side menu.

  bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
       @Override
       public boolean onNavigationItemSelected(@NonNull MenuItem item) {
           navController= Navigation.findNavController(HomeActivity.this, R.id.frag_container);
            switch (item.getItemId()) {
               case R.id.bottom_navi_intake:
                   navController.navigate(R.id.action_WeightFragment_to_IntakeFragment);
                   break;
               case R.id.bottom_navi_weight:
                   navController.navigate(R.id.action_IntakeFragment_to_WeightFragment);
                   break;
           }
           return true;
       }
   });


R.id.action_... are the action id in
@navigation/nav.graph.xml:

<?xml version="1.0" encoding="utf-8"?>
<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/nav_graph"
app:startDestination="@id/bottom_navi_intake">

<fragment
    android:id="@+id/bottom_navi_intake"
    android:name="com.example.dietdiary.IntakeFragment"
    android:label="@string/first_fragment_label"
    tools:layout="@layout/fragment_intake">

    <action
        android:id="@+id/action_IntakeFragment_to_WeightFragment"
        app:destination="@id/bottom_navi_weight" />
</fragment>
<fragment
    android:id="@+id/bottom_navi_weight"
    android:name="com.example.dietdiary.WeightFragment"
    android:label="@string/second_fragment_label"
    tools:layout="@layout/fragment_weight">

    <action
        android:id="@+id/action_WeightFragment_to_IntakeFragment"
        app:destination="@id/bottom_navi_intake" />
</fragment>


My final screenshot for your reference:
final outcome

like image 696
helen Avatar asked Oct 24 '25 01:10

helen


1 Answers

I had the same Error,I resolved it by Creating my Own Toolbar and then setting it to the the Support Action Bar as follows.

Initially,Get the toolbar with findviewbyid or by using databinding and assign it to a variable.Here I had set it to the variable toolbar and then further used it.

setSupportActionBar(toolbar)

And then We have to add it to the appbarconfiguraion as follows.

toolbar.setupWithNavController(navController, appBarConfiguration)

This error might happen due to an unestablished connection between Toolbar and the Navigation Views .

Hope this helps.

like image 51
Karunesh Palekar Avatar answered Oct 26 '25 16:10

Karunesh Palekar