DialogFragment property setCancelable does not work

I work in an Android application and use DialogFragment to show a dialog, and I want this dialog not to be canceled. I made the cancelable cancel property set to false, but still it does not affect.

Please take a look at my code and offer me a solution.

public class DialogTest extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return super.onCreateDialog(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.dialog_test, container, true); getDialog().requestWindowFeature(STYLE_NO_TITLE); getDialog().setCancelable(false); return view; } } 
+73
android android-dialogfragment
May 10 '13 at 10:22
source share
3 answers
 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.dialog_test, container, true); getDialog().requestWindowFeature(STYLE_NO_TITLE); getDialog().setCancelable(false); return view; } 

instead of getDialog().setCancelable(false); you need to use setCancelable(false); directly setCancelable(false);

so the updated answer will be like this:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.dialog_test, container, true); getDialog().requestWindowFeature(STYLE_NO_TITLE); setCancelable(false); return view; } 
+168
May 10 '13 at 10:46
source share

Use the following snippet

 void showDialog() { DialogFragment newFragment = MyAlertDialogFragment.newInstance( R.string..alert_dialog_two_buttons_title); newFragment.setCancelable(false); newFragment.show(getFragmentManager(), "dialog"); } 

and if you want to disable external touch around the dialog, use the following line of code

 DialogFragment.getDialog().setCanceledOnTouchOutside(true); 
+40
May 10 '13 at 10:44
source share

If you use the alert builder (and probably in each case you exchange the dialog inside the DialogFragment dialog box) to help you create the dialog, do not use getDialog (). setCancelable (false) or Dialog.setCancelable (false) because it will not work. Use setCancelable (false) as shown below in the code, as indicated in the official Android documentation:

 public void setCancelable (boolean cancelable) 

Added to API level 11 Control the cancellation of the displayed dialog. Use this instead of directly calling Dialog.setCancelable (boolean), because DialogFragment should change its behavior based on this. "

ref: http://developer.android.com/reference/android/app/DialogFragment.html#setCancelable(boolean)

 public class MyDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.dialog_layout, null, false); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) .setTitle("in case you want use a title").setView(view); AlertDialog alert = builder.create(); // alert.setCancelable(false); <-- dont' use that instead use bellow approach setCancelable(false); <- press back button not cancel dialog, this one works fine alert.setCanceledOnTouchOutside(false); <- to cancel outside touch return alert; } 
+24
Aug 16 '14 at 9:23
source share



All Articles