-2

I have an expandable ListView in my MainActivity. When certain item on the child ListView is selected some buttons (install, detail, cancel) are shown in fragment.

lv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {


                FragmentManager fragmentManager = getSupportFragmentManager();
                final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                if(!status){
                    final Options op = new Options();
                    fragmentTransaction.replace(R.id.main,op);
                    fragmentTransaction.commit();

                }

Here Options is my fragment's name. Pressing the cancel button in the Options class i want to remove this fragment and display only the MainActivity.

Button btnCancel = (Button) view.findViewById(R.id.btnCancel);
      btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           //REMOVE THIS FRAGMENT

        }
    });

Can anybody please tried help me?

Gowtham Subramaniam
  • 3,358
  • 2
  • 19
  • 31
Sudeep
  • 129
  • 10
  • @AmitVaghela In that ques the fragment is shown and hidden from same class. but in my ques the fragment is shown by clicking on child listview on main activity and removed from the fragment class using cancel button. – Sudeep Dec 08 '17 at 06:25

1 Answers1

1

To Remove all Fragments

public void clearAllFragments() {

    try {
        FragmentManager aFragmentManager = Context
            .getSupportFragmentManager();

        aFragmentManager.popBackStack(null,
                FragmentManager.POP_BACK_STACK_INCLUSIVE);
    } catch (Exception e) {

        e.printStackTrace();
    }
}

To Remove a single Fragment

public void removeFragment(int aCount) {

    FragmentManager aFragmentManager = Context
            .getSupportFragmentManager();

    for (int i = 0; i < aCount; i++) {

        aFragmentManager.popBackStack();
    }
}
Gowtham Subramaniam
  • 3,358
  • 2
  • 19
  • 31