POP_BACK_STACK_INCLUSIVE cause to call onResume method of previous fragment

1.5k views Asked by At

I have fragments say CompanyFragment,DashboardFragment,CalendarFragment. My default fragment is CompanyFragment.

Expected Flow Should be

  • Select Company from CompanyFragment
  • Navigating to DashboardFragment
  • Changing date from Dashboard so navigating to CalenderFragment
  • After selecting date from CalenderFragment should come back to DashboardFragment

Now I have three fragments added on Fragment manager CompanyFragment -> DashboardFragment -> CalenderFragment

I am using following code snippet to add/replace fragment

public void setFragmentToContainer(Fragment fragment) {

        final String tag = fragment.getClass().getName();
        if (manager == null) {
            manager = getSupportFragmentManager();
        }
        transaction = manager.beginTransaction();
        if (isFragmentInBackstack(manager, tag)) {
            if (fragment instanceof DayPartStoreListingFragment) {
            } else {
                manager.popBackStackImmediate(tag, manager.POP_BACK_STACK_INCLUSIVE);
            }
        } else {
            // Fragment doesn't exist
        }
        if (fragment instanceof CalenderFragment) {
            transaction.setCustomAnimations(R.anim.slide_up, 0, 0, R.anim.slide_down);
        }
        if (fragment instanceof DayPartStoreListingFragment) {
            transaction.add(R.id.layout_content, fragment, tag);
        } else {
            transaction.replace(R.id.layout_content, fragment, tag);
        }
        if (fragment instanceof CompanyLevelFragment) {
            //Exit app on back press
        } else {
            transaction.addToBackStack(tag);
        }
        transaction.commit();
    }

To check fragment entries in fragmentmanager I am using following code

public static boolean isFragmentInBackstack(final FragmentManager fragmentManager, final String fragmentTagName) {
    for (int entry = 0; entry < fragmentManager.getBackStackEntryCount(); entry++) {
        if (fragmentTagName.equals(fragmentManager.getBackStackEntryAt(entry).getName())) {
            return true;
        }

    }
    return false;
}

Problem is: When I tried to open DashboardFragment from CalendarFragment then for the first time onResume of CompanyFragment gets called and then DashboardFragment onResume gets called. Note: I haven't called CompanyFragment from CalenderFragment,CompanyFragments onResume Automatically gets called before onResume of DashboardFragmet. The same case happening for another fragments also. After date change onResume of previous fragment gets called first and then onResume on requested fragment.

After debugging i found issue in manager.popBackStackImmediate(tag, manager.POP_BACK_STACK_INCLUSIVE);

if I remove this line from code then above scenario not getting reproduced. But I need this line to clear older entries of same fragment from backstack. I really need help.

2

There are 2 answers

1
Bipin Bharti On

Step 1 : Used Add method for Fragment transaction not used replace

 FragSignUp FragSignUp = new FragSignUp();
        FragmentTransaction ft = root.getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
        ft.addToBackStack(FragSignUp.class.getName());
        ft.add(R.id.containerLogin, FragSignUp, FragSignUp.class.getName());
        ft.commit();

Step 2:Write onBAckPressed on Main Activity of Fragment

 FragSignIn fragSignIn=(FragSignIn) getSupportFragmentManager().findFragmentByTag(FragSignIn.class.getName());
    if(fragSignIn!=null && fragSignIn.isVisible()){
        fragSignIn.onResume();
    }else {
        super.onBackPressed();
    }

May be this code will be work try it ...

0
Reena On

Following is the code snippet helped to figure out this issue

    private void setFragmentToContainer(Fragment fragment) {
        try {
            String fragmentTag = fragment.getClass().getName();

            manager = getSupportFragmentManager();
            boolean fragmentPopped = manager.popBackStackImmediate(fragmentTag, 0);

            if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null) { //fragment not in back stack, create it.
                FragmentTransaction ft = manager.beginTransaction();
                ft.replace(R.id.layout_content, fragment, fragmentTag);

                ft.addToBackStack(fragmentTag);
                ft.commit();
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
      }
}