Remove fragment from activity when clicking button?

14,383

Solution 1

try this first add the fragment to backstack like this

FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(..............);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit(); 

Then remove the fragment like this:-

FragmentManager fm = getActivity().getSupportFragmentManager();
      if(fm.getBackStackEntryCount()>0) {
      fm.popBackStack();
}

to remove all fragments

FragmentManager fm = getActivity().getSupportFragmentManager();
      for (int i = 0; i < fm.getBackStackEntryCount(); ++i) {
      fm.popBackStack();
}

Solution 2

try this

getActivity().getSupportFragmentManager().popBackStack();

Solution 3

Use getFragmentManager().popBackStack();

Share:
14,383
user2573690
Author by

user2573690

Updated on June 14, 2022

Comments

  • user2573690
    user2573690 almost 2 years

    Currently, my main activity has a description field which opens a fragment when the user clicks on the description. On the fragment there is a text field and a button, when I click the button, I want to close the fragment and go back to my activity.

    How can I achieve this?

    I have added an onClickListener to my fragment to capture the click on the button. The toast message gets printed, but the fragment is not removed/closed.

    descDismiss.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getContext(), "Dismissed", Toast.LENGTH_LONG).show();
    
                    getActivity().getFragmentManager().popBackStackImmediate();
                }
            });
    

    I have the onClickListener in the onCreateView of the fragment. Is this correct?

    Thanks in advance!

    EDIT:

    I am adding my fragment like this:

    ((MainActivity)context).getSupportFragmentManager().beginTransaction()
                            .add(R.id.fragment_container, frag).commit();
    
    • Shaishav
      Shaishav over 7 years
      Are you sure you are using the proper fragment and fragment manager classes (not support one or the other way around)?
    • OneCricketeer
      OneCricketeer over 7 years
      A Fragment is inside an Activity, so please clarify "go back to your Activity".
    • Vishal Thakkar
      Vishal Thakkar over 7 years
      Are you using AppCompactActivity? then use getSupportFragmentManager
    • user2573690
      user2573690 over 7 years
      @Shaishav you are correct, I should be using the support fragment manager, I have made the change but it still does not work.
    • user2573690
      user2573690 over 7 years
      @cricket_007 I have a MainActivity which has a layout with a bunch of textviews and buttons, when the user clicks a certain button, a fragment is added overtop of everything. Now this fragment has a dismiss button which I want to close the fragment and go back to my MainActivity layout, does that help?
  • user2573690
    user2573690 over 7 years
    I have tried this and unfortunately it does not remove the fragment. See my edited post for how I am adding the fragment, maybe I am not doing that part correctly?
  • user2573690
    user2573690 over 7 years
    Thanks! I think my issue was that I wasn't adding the fragment to the backstack correctly. After that change the popBackStack worked.