-1

Here's my Flow/Structure:

  • Activity
  • Fragment
  • Same Fragment within it.

I have overriden removeCurrentFragment() method which does go back functionality by removing Fragment by ID as follows -

 @Override
    public void removeCurrentFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment currentFrag = getSupportFragmentManager().findFragmentById(R.id.fragment);

        if (currentFrag != null)
            transaction.remove(currentFrag);

        transaction.commit();
    }

It seems, when this happens, as ID of both fragment is same, it creates blank view.

If more details are required, please feel free to ask in comments. I will edit question as per required details.

I thought it was related to remove fragment, But I made new fragment with separate XML, still I am having the same issue.

I have parent fragment with pagination (as I need horizontal page scroll) in it have the child fragment, when clicking on child fragment button new fragment is replaced so when it is removed the X and Y of the child fragment is also disturbed.

here is the before and after screenshot of the x and y axis of child fragment. Before enter image description here After enter image description here

can any one suggest what could be the issue?

Sam Shaikh
  • 1,596
  • 6
  • 29
  • 53

1 Answers1

1

You have to use a tag to load the Fragment, and then use that same tag again to remove it.

For example, to add the Fragment:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
fragmentManager.popBackStack(tag, androidx.fragment.app.FragmentManager.POP_BACK_STACK_INCLUSIVE);
ft.setCustomAnimations(R.anim.enter, R.anim.exit);
ft.add(containerId, fragment, tag);
ft.addToBackStack(tag);
ft.commit();

To remove it, again use the same tag:

if (fragmentManager.findFragmentByTag(currentTag) != null) {
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.remove(fragmentManager.findFragmentByTag(currentTag));
    ft.commit();
}
Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • I tried this answer, but it's not working. I have modified question, which may help to narrow down problem. – Sam Shaikh May 01 '19 at 06:46