Issue
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;
}
}
}
}
Solution
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();
Answered By - Apurva
Answer Checked By - Pedro (JavaFixing Volunteer)