Saturday, 20 February 2021

BottomNavigationView and Android Navigation not switching fragments

I am trying to use a BottomNavigationView with Android Navigation to create a standard tabbed application with three fragments: StatusFragment, MapFragment, and AlertsFragment.

I have my main_activity.xml with a FragmentContainerView and BottomNavigationView defined as follows:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.main.MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/mainNavHostFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/bottomNavigationView"
        app:defaultNavHost="true"
        app:navGraph="@navigation/main_nav_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:menu="@menu/main_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

My main_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/main_nav_graph"
    app:startDestination="@id/mainActivity">
    <fragment
        android:id="@+id/statusFragment"
        android:name="<redacted>.ui.status.StatusFragment"
        android:label="StatusFragment" />
    <fragment
        android:id="@+id/mapFragment"
        android:name="<redacted>.ui.map.MapFragment"
        android:label="MapFragment" />
    <fragment
        android:id="@+id/alertsFragment"
        android:name="<redacted>.ui.alert.AlertsFragment"
        android:label="AlertsFragment" />
    <activity
        android:id="@+id/mainActivity"
        android:name="<redacted>.ui.main.MainActivity"
        android:label="main_activity"
        tools:layout="@layout/main_activity" />
</navigation>

And the associated main_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/statusFragment"
        android:icon="@drawable/ic_status_24"
        android:title="@string/title_status" />
    <item
        android:id="@+id/mapFragment"
        android:icon="@drawable/ic_map_24"
        android:title="@string/title_map" />
    <item
        android:id="@+id/alertsFragment"
        android:icon="@drawable/ic_alert_24"
        android:title="@string/title_alerts" />

</menu>

In the onCreate method of my MainActivity, I configure navigation as follows:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        binding = MainActivityBinding.inflate(layoutInflater)
        val navHostFragment = supportFragmentManager.findFragmentById(R.id.mainNavHostFragment) as NavHostFragment
        val navController = navHostFragment.navController

        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.statusFragment,
                R.id.mapFragment,
                R.id.alertsFragment
            )
        )

        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)
        NavigationUI.setupWithNavController(binding.bottomNavigationView, navController)
    }

The tab bar is displayed as expected, but tapping on the buttons does not do anything. I've made sure that the ids defined in main_menu.xml match those in main_nav_graph.xml. For being such a basic component of navigation on mobile, getting this to work on Android is proving to be frustratingly difficult; any help would be greatly appreciated.



from BottomNavigationView and Android Navigation not switching fragments

No comments:

Post a Comment