Home > OS >  DialogFragment SetCancelable(false) not working
DialogFragment SetCancelable(false) not working

Time:11-20

Please help me guys. I tried to put SetCancelable(false) in dialogfragment but still not working.

this is my DialogFragment:

    public static class UsageAcessDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setMessage("Grant Usage Access permission")
            .setTitle("Usage Access Permission")
            .setCancelable(false)
            .setPositiveButton("Allow", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // FIRE ZE MISSILES!
                    startActivityForResult(
                        new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS),
                        MY_PERMISSIONS_REQUEST_PACKAGE_USAGE_STATS);
                }
            });

        // Create the AlertDialog object and return it
        return builder.create();
    }
}

Thank you for those who will comment.

CodePudding user response:

You dont need to builder! you are inside dialog fragment try this one setCancelable(false) insted of using builder.setCancelable(false)

CodePudding user response:

You don't need to associate setCancelable with the builder. write setCancelable method directly because you are inside a DialogFragment

   AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
            builder.setTitle("Usage Access Permission");
            builder.setMessage("Grant Usage Access permission");
            setCancelable(false); // this line
            builder.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    tartActivityForResult(
                            new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS),
                            MY_PERMISSIONS_REQUEST_PACKAGE_USAGE_STATS);
                }
            });
    
            return builder.create();
        }
  • Related