0

I have an issue with my BottomSheetDialogFragment whereby I can cancel it with a touch outside or a back click but if I close the app and re-open it then those that I have canceled reappear.

I have tried adding cancelled on touch outside in the onCreateView but this hasn't helped as:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.new_item, container, false);
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        getDialog().setCancelable(true);
        getDialog().setCanceledOnTouchOutside(true);
        return view;
    }

Any ideas appreciated! I have a feeling it is something to do with the dismiss but I'm not sure.

The full code is:


public class AddNewItem extends BottomSheetDialogFragment {

    public static final String TAG = "ActionBottomDialog";

    private EditText newItemText;
    private Button newItemSaveButton;
    private DatabaseHandler db;

    public static AddNewItem newInstance(){
        return new AddNewItem();
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL, R.style.DialogStyle);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.new_item, container, false);
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        getDialog().setCancelable(true);
        getDialog().setCanceledOnTouchOutside(true);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){
        super.onViewCreated(view, savedInstanceState);
        newItemText = getView().findViewById(R.id.newItemText);
        newItemSaveButton = getView().findViewById(R.id.newItemButton);

        db = new DatabaseHandler(getActivity());
        db.openDatabase();

        // changes whether updating a task or not
        boolean isUpdate = false;

        final Bundle bundle = getArguments();
        if(bundle != null){
            isUpdate = true;
            String item = bundle.getString("item");
            newItemText.setText(item);
            if(item.length()>0){
                newItemSaveButton.setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
            }
        }
        newItemText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                if (s.toString().equals("")) {
                    newItemSaveButton.setEnabled(false);
                    newItemSaveButton.setTextColor(Color.GRAY);
                }
                else{
                    newItemSaveButton.setEnabled(true);
                    newItemSaveButton.setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        final boolean finalIsUpdate = isUpdate;
        newItemSaveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                String text = newItemText.getText().toString();
                if(finalIsUpdate){
                    db.updateItem(bundle.getInt("id"), text);
                }
                else{
                    ShoppingModel item = new ShoppingModel();
                    item.setItem(text);
                    item.setStatus(0);
                    db.insertItem(item);
                }
                dismiss();
            }
        });

    }


    @Override
    public void onDismiss(DialogInterface dialog){
        Activity activity = getActivity();
        if(activity instanceof DialogCloseListener){
            ((DialogCloseListener)activity).handleDialogClose(dialog);
        }
    }

}
Philip09
  • 85
  • 9

2 Answers2

0

If you want to prevent the BottomSheetDialogFragment from being re-shown after the app has been closed and re-opened, you will need to handle the saved instance state in your onCreateView method and check whether the fragment has previously been canceled. If it has, you can simply return null from the onCreateView method to prevent the fragment from being shown again.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    if (savedInstanceState != null && savedInstanceState.getBoolean("is_cancelled")) {
        return null;
    }

    View view = inflater.inflate(R.layout.new_item, container, false);
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    getDialog().setCancelable(true);
    getDialog().setCanceledOnTouchOutside(true);
    return view;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean("is_cancelled", true);
}
Vaibhav
  • 117
  • 7
0

This was resolved by overriding the onCancel:

@Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
        dismiss();
    }

From: Detect back button but don't dismiss dialogfragment

And by adding an on click listener in onViewCreated for touch outside:

View touchOutsideView = getDialog()
                .getWindow()
                .getDecorView()
                .findViewById(com.google.android.material.R.id.touch_outside);

        touchOutsideView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
Philip09
  • 85
  • 9