选择底部导航视图上的项目以替换片段时关闭
App closes when item on bottom navigation view is selected to replace fragment
当我在底部导航栏中添加 select 项目时,我的应用程序关闭,没有崩溃,只是进入后台。我正在使用 material 按钮导航视图,但问题仍然存在,所以我决定使用 AHBottomNavigation 库,但问题仍然存在。
设置底部导航代码
AHBottomNavigation bottomNavigation = findViewById(R.id.navigation);
AHBottomNavigationItem item1 = new AHBottomNavigationItem(getString(R.string.orders), R.drawable.ic_m_orders);
AHBottomNavigationItem item2 = new AHBottomNavigationItem(getString(R.string.pickup), R.drawable.ic_m_location);
bottomNavigation.setAccentColor(Color.parseColor("#2DA1D1"));
bottomNavigation.setInactiveColor(Color.parseColor("#C4C4C4"));
bottomNavigation.addItem(item1);
bottomNavigation.addItem(item2);
bottomNavigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
bottomNavigation.setOnTabSelectedListener(tabSelectedListener);
bottomNavigation.setBehaviorTranslationEnabled(true);
openFragment(fragmentHolder);
private void openFragment(final Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.host_fragment, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
private final AHBottomNavigation.OnTabSelectedListener tabSelectedListener = (position, wasSelected) -> {
switch (position) {
case 0:
openFragment(fragmentHolder);
break;
case 1:
openFragment(PickupStationFragment.newInstance());
break;
}
return true;
};
xml代码
<androidx.fragment.app.FragmentContainerView
android:layout_marginBottom="56dp"
android:id="@+id/host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="@id/cl"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="@color/white"
android:visibility="visible"
android:layout_gravity="bottom"/>
我找到了解决方案,我在我的一个片段上覆盖了 ondestroy 视图,并在覆盖的方法中添加了 requireActivity.finish()
当我在底部导航栏中添加 select 项目时,我的应用程序关闭,没有崩溃,只是进入后台。我正在使用 material 按钮导航视图,但问题仍然存在,所以我决定使用 AHBottomNavigation 库,但问题仍然存在。
设置底部导航代码
AHBottomNavigation bottomNavigation = findViewById(R.id.navigation);
AHBottomNavigationItem item1 = new AHBottomNavigationItem(getString(R.string.orders), R.drawable.ic_m_orders);
AHBottomNavigationItem item2 = new AHBottomNavigationItem(getString(R.string.pickup), R.drawable.ic_m_location);
bottomNavigation.setAccentColor(Color.parseColor("#2DA1D1"));
bottomNavigation.setInactiveColor(Color.parseColor("#C4C4C4"));
bottomNavigation.addItem(item1);
bottomNavigation.addItem(item2);
bottomNavigation.setTitleState(AHBottomNavigation.TitleState.ALWAYS_SHOW);
bottomNavigation.setOnTabSelectedListener(tabSelectedListener);
bottomNavigation.setBehaviorTranslationEnabled(true);
openFragment(fragmentHolder);
private void openFragment(final Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.host_fragment, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
private final AHBottomNavigation.OnTabSelectedListener tabSelectedListener = (position, wasSelected) -> {
switch (position) {
case 0:
openFragment(fragmentHolder);
break;
case 1:
openFragment(PickupStationFragment.newInstance());
break;
}
return true;
};
xml代码
<androidx.fragment.app.FragmentContainerView
android:layout_marginBottom="56dp"
android:id="@+id/host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="@id/cl"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="@color/white"
android:visibility="visible"
android:layout_gravity="bottom"/>
我找到了解决方案,我在我的一个片段上覆盖了 ondestroy 视图,并在覆盖的方法中添加了 requireActivity.finish()