-3

I have a problem

I have a non cancelable custom dialog which mean this custom dialog can only close if presses buton inside the custom dialog, so it won't cancel on backpress or click outside

I tried setCancelable(false) and it works however in my activity I have a onBackPressed and whenever my non cancelable dialog show onBackPressed wont trigger when I click back button because I think they conflict

is their a solution to do this?

EDIT: The purpose is I want the user to click button ok, or skip inside the custom dialog which means this dialog is required before proceeding to next activity

also in onBackPressed since I am using fragment whenever user press back it changes to previous fragment

sorry for lacking of explanation

my code is this

Dialog

dialog_welcome_navigation = DialogUtils.showCustomDialog(context, R.layout.dialog_welcome_navigation);
dialog_welcome_navigation.setCancelable(false); // disable closing dialog with back pressed
dialog_welcome_navigation.setCanceledOnTouchOutside(true);

and the onBackPressed

@Override
public void onBackPressed(){
    Log.d("TAG", "--back--");
}
Beginner
  • 3,821
  • 3
  • 14
  • 24

1 Answers1

1

After searching I have found a solution thanks to this SO answer

https://stackoverflow.com/a/25251122/3481654

I add a setOnKeyListener on my dialog

dialog_welcome_navigation.setOnKeyListener(dialogWelcomeNavigationOnKey);

private DialogInterface.OnKeyListener dialogWelcomeNavigationOnKey = new DialogInterface.OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == android.view.KeyEvent.KEYCODE_BACK) {

            dialog_welcome_navigation.dismiss();

            // move other fragment

            return true;
        }
        return false;
    }
};
Beginner
  • 3,821
  • 3
  • 14
  • 24
  • did you read this comment https://stackoverflow.com/questions/46462203/setcancelablefalse-and-onbackpressed-conflict#comment79879308_46462203 – AskNilesh Sep 28 '17 at 06:51