Skip to content

Commit

Permalink
Applied non-button dismiss fix to Confirm Dialog
Browse files Browse the repository at this point in the history
[Fix][Mod]: Confirm Dialog now sends a negative button press whenever
cancelled. Additionally, on click interface now dismisses automatically.
  • Loading branch information
iamovrhere committed Oct 10, 2014
1 parent f0f0266 commit 086e2ec
Showing 1 changed file with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
/** Simple confirmation dialog that retains it's instance.
* Give a title, message, positive, negative buttons.
* Requires either activity to implement
* DialogInterface.OnClickListener or
* call {@link Builder#setTargetFragment(Fragment, int)}
* {@link DialogInterface#OnClickListener} or
* call {@link Builder#setTargetFragment(Fragment, int)}.
* Note that upon a given button press, the dialog dismisses itself.
* @author Jason J.
* @version 0.1.0-20140923
* @version 0.2.0-20141010
*/
public class ConfirmationDialogFragment extends DialogFragment
implements DialogInterface.OnClickListener {
implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener {
/** Class name for debugging purposes. */
final static private String CLASS_NAME = ConfirmationDialogFragment.class
.getSimpleName();
Expand Down Expand Up @@ -62,7 +63,7 @@ public class ConfirmationDialogFragment extends DialogFragment

/** The optional listener that is sometimes activity. Can be null. */
private DialogInterface.OnClickListener mListener = null;

protected ConfirmationDialogFragment() {}

@Override
Expand Down Expand Up @@ -102,6 +103,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(positiveResource, this)
.setNegativeButton(negativeResource, this)
.setOnCancelListener(this)
.create();
}

Expand All @@ -110,8 +112,9 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
*/
@Override
public void onDestroyView() {
if (getDialog() != null && getRetainInstance())
if (getDialog() != null && getRetainInstance()){
getDialog().setOnDismissListener(null);
}
super.onDestroyView();
}

Expand Down Expand Up @@ -184,23 +187,29 @@ public DialogFragment create(){

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if (mListener != null && getTargetFragment() == null){
//only if we have a listener and target fragment.
mListener.onClick(dialog, which);
mListener.onClick(dialog, which);
return;
}
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
getTargetFragment().onActivityResult(getTargetRequestCode(),
Activity.RESULT_OK, null);
dialog.dismiss();
Activity.RESULT_OK, null);
break;
case DialogInterface.BUTTON_NEGATIVE:
getTargetFragment().onActivityResult(getTargetRequestCode(),
Activity.RESULT_CANCELED, null);
dialog.dismiss();
break;
}
}

@Override
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
//if cancelled, send negative click
onClick(getDialog(), DialogInterface.BUTTON_NEGATIVE);
}

}

0 comments on commit 086e2ec

Please sign in to comment.