0

I have a small app with on activity and two fragments inside. The fragments are loaded with the BottomNavitationView.

MonitoringFragment gets loaded on the OnCreate in the activity.

I want to add this one to the backstack so when I'm inside the second fragment (ConnectionFragment) and i press back I get to the first fragment. This works fine. However the BotttonNavigationView doesn't get updated (doesn't set the first item as selected when returning from second fragment. picture 3). I assume it doesn't handle this behavior by itself and I have to implement it myself but every attempt I made was unsuccessfull.

MonitoringFragment ConnectionFragment MonitoringFragment coming from backstack

Activity code:

Fragment activeFragment = null;
BottomNavigationView bottomNavigationView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bottomNavigationView = findViewById(R.id.bottom_navigation);

    bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
        switch (item.getItemId()) {
            case R.id.monitoring:
                setCurrentFragment(new MonitoringFragment(), false);
                break;
            case R.id.connection:
                setCurrentFragment(new ConnectionFragment(), true);
                break;
        }
        return true;
    });
    setCurrentFragment(new MonitoringFragment(), true);
}

private void setCurrentFragment(Fragment fragment, boolean addToBackStack) {
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fragment);
    if (addToBackStack) {
        fragmentTransaction.addToBackStack(null);
    }
    fragmentTransaction.commit();
    activeFragment = fragment;
}

Thanks!

FerranLP
  • 21
  • 2

2 Answers2

0

Yes, BottomNavigationView is not setup with fragmentManager. You might set prop select tab by yourself, when a fragment gets resumed.

Or you can use navigation component with BottomNavigationView, those working ok together.

With the second approach when pressing back button will not return you from 2nd fragment to 1st one.

Maksym V.
  • 2,877
  • 17
  • 27
0

Faced a similar problem, here is how I solved it

I added OnDestinationChangedListener to my navcontroller, and created several arraylists with destination label of fragments in my progect.

When current destination is in one of these fragments, bottom menu button is checked

I hope the code example is clearer than my explanation)

 val destListNews = arrayListOf(
        "news_fragment",
        "NewsViewingFragment"
    )

    val destListAds = arrayListOf(
        "ads_view_fragment",
        "ads_fragment",
        "AdsAddFragment"
    )

    val destListPass = arrayListOf(
        "pass_fragment",
        "pass_creation_fragment",
        "PassViewFragment"
    )

    val destListVotes = arrayListOf(
        "VotesListFragment",
        "VotesAddFragment",
        "ChooseVoteTypeFragment"
    )

    navController.addOnDestinationChangedListener { controller, destination, arguments ->

        when (destination.label) {

            in destListNews -> {
                bottom_nav.menu.getItem(0).isChecked = true
            }

            in destListAds -> {
                bottom_nav.menu.getItem(1).isChecked = true
            }

            in destListPass -> {
                bottom_nav.menu.getItem(2).isChecked = true
            }

            in destListVotes -> {
                bottom_nav.menu.getItem(3).isChecked = true
            }

            else -> {
                bottom_nav.menu.getItem(4).isChecked = true
            }
        }
    }
Guahoo
  • 19
  • 7