Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a fragment from within itself

In my application, I have an instance of a fragment, ActivityFragment, which is added dynamically when a button, addActivity, is pressed. There is a delete_button in each ActivityFragment, and I have set an onClickListener for this button within the ActivityFragment class. When the delete_button is pressed, I want to remove the fragment from inside the onClick method. How would I go about doing this when I create the ActivityFragment object and add it to the activity in a method outside of the fragment class? And what field should I use for .remove()?

Note that delete_button should only remove the instance of the fragment it is in.

Here is my MainActivity.java with the ActivityFragment class. The addActivity button is at the bottom:

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public static class ActivityFragment extends Fragment {
        // @Nullable is used because the method may return a null value
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            final View fragment1 = inflater.inflate(R.layout.activity_fragment, container, false);

            Button edit_button = (Button) fragment1.findViewById(R.id.edit_button);
            Button delete_button = (Button) fragment1.findViewById(R.id.delete_button);

            edit_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TextView activityText = (TextView) getView().findViewById(R.id.activity_text);
                    activityText.setText("success");
                }
            });

            delete_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getFragmentManager().beginTransaction()
                            .remove().commit();
                }
            });

            return fragment1;
        }
    }

    public void addActivity(View view) {
        ActivityFragment myFragment = new ActivityFragment();
        getFragmentManager().beginTransaction()
                .add(R.id.fragment_container, myFragment).commit();
    }

}
like image 237
Ewen Crawford Avatar asked Sep 06 '25 02:09

Ewen Crawford


1 Answers

You can remove the fragment instance like this

delete_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getFragmentManager().beginTransaction()
                    .remove(ActivityFragment.this).commit();
        }
    });
like image 58
Ayush Khare Avatar answered Sep 08 '25 23:09

Ayush Khare