Taking this idea a little further, I created a listener interface inside the dialog and implemented it in the main activity.
public interface OnDialogResultListener {
public abstract void onPositiveResult(String value);
public abstract void onNegativeResult();
}
public void setOnDialogResultListener(OnDialogResultListener listener) {
this.onDialogResultListener = listener;
}
Call onNegativeResult() inside an overriden onCancel(DialogInterface)
and onPositiveResult(String)
where you want your dialog to return the value.
Note: don't forget to dismiss()
your dialog after calling onPositiveResult()
or the dialog window will stay opened.
Then inside your main activity you can create a listener for the dialog, like so:
QuantityDialogFragment dialog = new QuantityDialogFragment();
dialog.setOnDialogResultListener(new QuantityDialogFragment.OnDialogResultListener() {
@Override
public void onPositiveResult(String value) {
//Do something...
}
@Override
public void onNegativeResult() {
//Do something...
}
});
This will make your dialog easier to reuse later.