Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I close the fragment?

I am have Fragment on the Activity. Fragment has button. if i click on the button, Fragment must be close. How i am did this?

public class ItemFragment extends Fragment{

    private ImageView btnApply;
    private ClickButton clickButton = new ClickButton();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.item_info, container, false);
        btnApply = (ImageView) rootView.findViewById(R.id.btnSendItem);
        btnApply.setOnClickListener(clickButton);
        return rootView;
    }

    private class ClickButton implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if (R.id.btnSendItem == v.getId()) {
                Toast.makeText(getActivity(),"CLOSE",Toast.LENGTH_LONG).show();
                return;
            }
        }
    }
}
like image 662
Pavel Petrashov Avatar asked Dec 20 '22 05:12

Pavel Petrashov


1 Answers

There's no such thing like close the fragment, but you can remove the fragment from the stack. To pop the fragment use the following inside button click listener

getActivity().getFragmentManager().beginTransaction().remove(this).commit();
like image 148
Apurva Avatar answered Dec 27 '22 10:12

Apurva