0

I'm hiding a bottom navigation item when user is not authenticated (that item will show up once the user is authenticated, which in this case, is a Messages Fragment). That works well, but when that fragment is shown and selected, the previous navigation menu that I was in is still highlighted, even if I'm on that fragment. Also, once I chose another navigation item, Messages is still highlighted on the bottom navigation bar. Refer to the screenshots below:

  1. When the Messages is not yet selected Home Fragment, Message Fragment not yet selected

  2. When I select Message Fragment (I was from Home Fragment before that) Message Fragment Selected (Previously, Home Fragment)

  3. When I go back to any other Fragment aside from Messages (in this case, Home Fragment) Home Fragment Selected (Previously, Message Fragment)

Here's my MainActivity (Notice that there's navView.menu.findItem(R.id.navigation_messages).isVisible, which is the main cause of this behavior in bottom navigation bar)

class MainActivity : AppCompatActivity() {

    private var authState : Int = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val navView: BottomNavigationView = findViewById(R.id.nav_view)
        val navController = findNavController(R.id.nav_host_fragment)

        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home, R.id.navigation_explore, R.id.navigation_messages,  R.id.navigation_account
            )
        )

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.

        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)

        when(authState) {
            0 -> navView.menu.findItem(R.id.navigation_messages).isVisible = false
            1 -> navView.menu.findItem(R.id.navigation_messages).isVisible = true
        }
    }

    override fun onSupportNavigateUp(): Boolean {
        return navigateUp(Navigation.findNavController(this, R.id.nav_host_fragment), AppBarConfiguration(setOf(
            R.id.navigation_home, R.id.navigation_explore, R.id.navigation_messages, R.id.navigation_account
        )))
    }
}

Also, here's my activity_main.xml file:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:itemHorizontalTranslationEnabled="false"
        app:labelVisibilityMode="labeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        android:layout_marginBottom="56dp"
        app:layout_constraintBottom_toTopOf="@+id/nav_view"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

To note, I'm using Android Jetpack Navigation Architecture Components, which made other similar solutions like this, this, or this, not that viable on this case.

kkshinichi
  • 63
  • 1
  • 2
  • 10
  • remove `android:background="?android:attr/windowBackground"` probabaly use itembackgorund and itemIconTint – Pavneet_Singh Mar 06 '20 at 16:15
  • Same problem persists, it just made the icon colors match the selected ones. Only the Text beneath the icon changes color (and size). – kkshinichi Mar 06 '20 at 16:38
  • After checking through other codes, I just ungrouped the Messages from the menu xml. That causes the always highlighted behavior of Messages – kkshinichi Mar 11 '20 at 10:45

0 Answers0