When I change a fragment, I would like to compare the new with the current. If they are the same, they do not need to be replaced.
I tried
public void displayFragment(Fragment fragment){ FragmentManager fm = getSupportFragmentManager(); Fragment currentFragment = fm.findFragmentById(R.id.content_frame); if(!currentFragment.equals(fragment)) getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.content_frame, fragment).commit(); }
but it doesnβt work, and the fragment changes even if the new one is the same
Decision? Thanks
EDIT
Decision:
public void displayFragment(Fragment fragment){ FragmentManager fm = getSupportFragmentManager(); Fragment currentFragment = fm.findFragmentById(R.id.content_frame); if(!fragment.getClass().toString().equals(currentFragment.getTag())){ getSupportFragmentManager() .beginTransaction() .addToBackStack(null) .replace(R.id.content_frame, fragment, fragment.getClass().toString())
source share