Remove fragment from activity when clicking button?
<p>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.</p>

<p>How can I achieve this?</p>

<p>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.</p>

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

                getActivity().getFragmentManager().popBackStackImmediate();
            }
        });
</code></pre>

<p>I have the onClickListener in the onCreateView of the fragment. Is this correct?</p>

<p>Thanks in advance!</p>

<h2>EDIT:</h2>

<p>I am adding my fragment like this:</p>

<pre><code>((MainActivity)context).getSupportFragmentManager().beginTransaction()
                        .add(R.id.fragment_container, frag).commit();
</code></pre>