【问题标题】:OnBackPressed() doesn't work properly in BottomNavigationViewOnBackPressed() 在 BottomNavigationView 中无法正常工作
【发布时间】:2020-06-03 13:09:30
【问题描述】:

我使用 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" />

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

和 loadFragment():

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

【问题讨论】:

  • 我很久没有和BottomNavigation 合作了,但我记得如果你只是改变Fragment 这并不意味着图标选择也会改变。您必须取消选择已选择的图标并选择应该选择的图标。这就是我一直这样做的方式。

标签: android fragment androidx bottomnavigationview onbackpressed


【解决方案1】:

在方法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
    }

【讨论】:

  • 当我删除这段代码时(transaction.addToBackStack(null)),第一次点击后退按钮将退出应用程序。我想回到上一个片段。
  • 当我使用 onAttachFragment 方法时,应用程序没有运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多