Saeid Parvizi
Saeid Parvizi

Reputation: 21

OnBackPressed() doesn't work properly in BottomNavigationView

I use BottomNavigationView But when I click on the back button, the menu icons doesn't change (but the fragments change). For example, I am on the home menu and I am moved to that menu by touching the settings menu, but when I click on the back button, I am moved to the home fragment, but the home icon is not selected and it is still the settings icon.

I have used the following libraries.

implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

And also in XML:

<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
    app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    app:labelVisibilityMode="labeled"
    android:id="@+id/bottomNavigationView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layoutDirection="rtl"
    android:background="@color/bgBottomNavigation"
    android:foreground="?attr/selectableItemBackground"
    app:itemBackground="@color/bgBottomNavigation"
    app:layout_constraintBottom_toBottomOf="@+id/constraint"
    app:menu="@menu/bottom_navigation"
    app:itemIconTint="@drawable/bnv_tab_item_foreground"
    app:itemTextColor="@drawable/bnv_tab_item_foreground"
    tools:ignore="MissingConstraints" />

And in menu :

<?xml version="1.0" encoding="utf-8"?>

<item
    android:enabled="true"
    android:id="@+id/navigation_home"
    android:icon="@drawable/ic_home_black_24dp"
    android:title="@string/title_home" />
<item
    android:enabled="true"
    android:id="@+id/navigation_cart"
    android:icon="@drawable/ic_shopping_cart_black_24dp"
    android:title="@string/title_cart" />
<item
    android:enabled="true"
    android:id="@+id/navigation_bookmark"
    android:icon="@drawable/ic_bookmark_black_24dp"
    android:title="@string/title_bookmark" />
<item
    android:enabled="true"
    android:id="@+id/navigation_profile"
    android:icon="@drawable/ic_settings_black_24dp"
    android:title="@string/title_profile" />

And in java:

bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
bottomNavigationView.setSelectedItemId(R.id.navigation_home);


private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment;
        actionId = item.getItemId();
        switch (item.getItemId()) {
            case R.id.navigation_home:
                fragment = new HomeFragment();
                loadFragment(fragment);
                return true;
            case R.id.navigation_cart:
                fragment = new CartFragment();
                loadFragment(fragment);
                return true;
            case R.id.navigation_bookmark:
                fragment = new BookmarkFragment();
                loadFragment(fragment);
                return true;
            case R.id.navigation_profile:
                fragment = new SettingsFragment();
                loadFragment(fragment);
                return true;

        }
        return false;
    }
};

And loadFragment():

private void loadFragment(Fragment fragment) {
    // load fragment
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_container, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

Upvotes: 0

Views: 330

Answers (1)

C&#244;ng Hải
C&#244;ng Hải

Reputation: 5261

Inside method loadFragment you already call transaction.addToBackStack(null) it means when you press back top Fragment will be pop and your menu still keep at current select.

  1. If you want finish when press back you should remove call transaction.addToBackStack(null)

  2. If you want to keep this behavior and you have to update menu, put this code inside your activity

    @Override
    public void onAttachFragment(@NonNull Fragment fragment) {
        super.onAttachFragment(fragment);
        if (fragment instanceof HomeFragment) {
            bottomNavigationView.setSelectedItemId(R.id.navigation_home);
        } else if (fragment instanceof CartFragment) {
            bottomNavigationView.setSelectedItemId(R.id.navigation_cart);
        } // others your fragments
    }

Upvotes: 1

Related Questions