0

I faced app with one activity and all navigation based on Fragments. I have one place containing next steps:

fragment1 replaced in container id1; I use add for add fragment2 to the same id1 container; after I use replace for add fragment3 to the same id1 container;

It is three undefended transactions. All are added to back stack. But when I press back, after last, fragment2 does not appear, I see only fragmtent1 on container id1, but fragment2 is in back stack, because next pressing back button has no effect fragment1 still visible, after next pressing back button fragment1 will be remove from container.

It looks like after first pressing back button fragment2 does not create its view and so he is disappearing on container.

There is a part of code I used for replace:

FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.kp_content_frame, fragment);

    if (addToBackStack) {
        transaction.addToBackStack(fragment.getTag());
    }
    if (allowingStateLoss) {
        transaction.commitAllowingStateLoss();
    } else {
        transaction.commit();
    }

For add:

final String tagToAdd = fragment instanceof BaseFragment ? ((BaseFragment) fragment).getCustomTag() : fragment.toString();

        if(isDuplicateFragment(tagToAdd)) {
            return;
        }

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        if (enterPopAnim != 0 || exitPopAnim != 0) {
            transaction.setCustomAnimations(enterAnim, exitAnim, enterPopAnim, exitPopAnim);
        } else if(enterAnim != 0 || exitAnim != 0) {
            transaction.setCustomAnimations(enterAnim, exitAnim);
        }

        transaction.add(R.id.kp_content_frame, fragment, tagToAdd);

        if (addToBackStack) {
            transaction.addToBackStack(tagToAdd);
        }
        transaction.commit();

Any ideas? Thanks.

busylee
  • 2,540
  • 1
  • 16
  • 35

1 Answers1

1

But when I press back, after last, fragment2 does not appear

Because you have two fragments in the container id1. And fragment1 is above fragment2.

I see only fragmtent1 on container id1, but fragment2 is in back stack, because next pressing back button has no effect fragment1 still visible,

Because next pressing back button removed fragment2, which were invisible.

Simple solution: don't mix add and replace transactions. Use replace everywhere.