-1

I have 4 fragment in home layout. I want to remove specific fragment when user press corresponding button. when i am doing this with popBackStack() it deleting recent fragment added by user. so how can i remove specific fragment from screen.

I am adding Fragment on Button Click, i have same 3 more add buttons.

addFragment_1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction()
                    .add(R.id.Fragment_container_1, new Fragment_1(), "Frag 1")
                    .addToBackStack("Frag 1")
                    .commit();
        }
    });

Then i want to remove this fragment with other button click. also add 3 more remove button.

 removeFragment_1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getSupportFragmentManager().popBackStack();
        }
    });

I have 4 add button and 4 remove button. when i add all 4 fragment on screen and then press the 1st remove button it remove 4th fragment then 3 > 2>1 and all 4 remove Button doing the same. I want 1 remove button delete only one corresponding fragment.

I also try popBackStack ("Frag 1", 0) it remove all 3 fragment except fragment 1, and when i try popBackStack("Frag 1", FragmentManager.POP_BACK_STACK_INCLUSIVE) it remove all 4 fragment.

I not that good in English, I want some detailed answer. thanks

neo
  • 71
  • 1
  • 9
  • Share your code for how you are performing deletion of fragment. This will help to answer the issue you have in your code – Jibran Khan Jun 17 '19 at 10:32

1 Answers1

0
Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
if(fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();

This must work.

vignesh
  • 492
  • 3
  • 9