0
votes

Hi guys here is my code

navigationView.setNavigationItemSelectedListener(new NavigationView
                .OnNavigationItemSelectedListener() {
            @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (item.isChecked()) {
                    //item already selected. Do nothing
                    drawerLayout.closeDrawer(GravityCompat.START);
                    return true;
                }
                switch (item.getItemId()) {
                    case R.id.home:
                        FragmentTransaction transaction = getSupportFragmentManager()
                                .beginTransaction();
                        transaction.replace(R.id.fragment, new HomeFragment())
                                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
                        break;
                    case R.id.other:
                        transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.fragment, new OtherFragment())
                                .addToBackStack(null).setTransition(FragmentTransaction
                                .TRANSIT_FRAGMENT_FADE).commit();
                        break;
                    default:
                        break;
                }
                drawerLayout.closeDrawer(GravityCompat.START);
                return true;
            }
        });

I basically only have 2 fragments and merely the HomeFragment should be added to the backstack. After switching between HomeFragment and OtherFragment for a while and clicking on the back button while on the HomeFragment, I end up with the HomeFragment getting displayed several times. Within the HomeFragment lies a recyclerview. When I scroll up and down I can really see that the rows are displayed multiple times. How can I make sure that the HomeFragment is added to the backstack only once.

Thanks

2

2 Answers

0
votes

When you click on the Home menu item try to find out whether there is any fragment in the backstack or not. If yes, call popBackStack() or replace a fragment as you do now otherwise. Use this code:

if (getSupportFragmentManager().getBackStackEntryCount() > 0) { getSupportFragmentManager().popBackStack(); }

1
votes

You could easily check your fragment with

YourFragment.isAdded

And if you have a multiple fragment you could create a new class to manage all the fragment and create state to check if fragment have been added or not.