Search code examples
androidbottomnavigationview

App closes when item on bottom navigation view is selected to replace fragment


My app closes, not crashing but just goes to the background,when i select item on the bottom navigation. i was using material botton navigation view but the problem persisted so i decided to use AHBottomNavigation library but the issue is still there.

Set up bottom navigation code

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

the xml code

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

Solution

  • I found the solution, i was overriding ondestroy view on one of my fragment and added requireActivity.finish() in the overridden method