OnBackPressed() 在 BottomNavigationView 中无法正常工作



我使用BottomNavigationView但是当我单击后退按钮时,菜单图标不会改变(但片段会改变(。 例如,我在主菜单上,通过触摸设置菜单移动到该菜单,但是当我单击后退按钮时,我被移动到主页片段,但主页图标未被选中,它仍然是设置图标。

我使用了以下库。

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'

而且在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" />

在菜单中:

<?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" />

在爪哇中:

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;
}
};

和 loadFragment((:

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

Inside 方法loadFragment您已经调用transaction.addToBackStack(null)这意味着当您按后顶部时Fragment会弹出,并且您的菜单仍保持当前选择状态。

  1. 如果要在按回finish时删除呼叫transaction.addToBackStack(null)

  2. 如果要保留此行为并且必须更新菜单,请将此代码放入您的活动中

@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
}

相关内容

  • 没有找到相关文章

最新更新