user2573690
user2573690

Reputation: 6013

Remove fragment from activity when clicking button?

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();

Upvotes: 2

Views: 15310

Answers (5)

tyrodeveloper
tyrodeveloper

Reputation: 131

My way to add fragments:

fragmentTransaction = fragmentManager.beginTransaction();
frmInicio fragment = new frmInicio();
fragmentTransaction.add(R.id.content_menu_layout, fragment,"frm_inicio");
fragmentTransaction.commit();

My way to remove

//frm_inicio
Fragment frm = fragmentManager.findFragmentByTag("frm_inicio");
if(frm!=null){
    fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.remove(frm);
    fragmentTransaction.commit();
}

Upvotes: 0

Jay Shah
Jay Shah

Reputation: 742

Use getFragmentManager().popBackStack();

Upvotes: 1

Jagjit Singh
Jagjit Singh

Reputation: 1969

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();
}

Upvotes: 9

Sangram Haladkar
Sangram Haladkar

Reputation: 717

try to using following code:

   public void fragmentReplace() {
            if (getSupportFragmentManager().findFragmentByTag(new TermsAndConditionFragment().getClass().getName()) != null) {
                getSupportFragmentManager().popBackStack(new TermsAndConditionFragment().getClass().getName(),
                        FragmentManager.POP_BACK_STACK_INCLUSIVE);
            }  else {
                super.onBackPressed();
            }

in main Activity in descDismiss button clickListener insert below code:

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

            }
        });

Upvotes: 0

Uma Achanta
Uma Achanta

Reputation: 3729

try this

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

Upvotes: 1

Related Questions