Skip to content

Instantly share code, notes, and snippets.

@SelvinPL
Last active May 25, 2022 09:00
Show Gist options
  • Select an option

  • Save SelvinPL/2c8e8ba2954f1ae204190501682e8039 to your computer and use it in GitHub Desktop.

Select an option

Save SelvinPL/2c8e8ba2954f1ae204190501682e8039 to your computer and use it in GitHub Desktop.
Dialogs callback
public interface GenericDialogFragment {
String MESSAGE = "MESSAGE";
String TITLE = "TITLE";
String URI = "URI";
String ID = "ID";
String ARGS = "ARGS";
String VIEW = "VIEW";
String DATE = "DATE";
String POSITIVE = "POSITIVE";
String NEGATIVE = "NEGATIVE";
String MAX_DATE = "MAX_DATE";
String MIN_DATE = "MIN_DATE";
String DIALOG_FRAGMENT_TAG = "DIALOG";
class PositiveNegative extends DialogFragment implements GenericDialogFragment {
public static OKCancel newInstance(int id, @StringRes int title, @StringRes int message, @StringRes int positive, @StringRes int negative, Parcelable argsIn) {
OKCancel frag = new OKCancel();
Bundle args = new Bundle();
args.putInt(ID, id);
args.putInt(TITLE, title);
args.putInt(MESSAGE, message);
args.putInt(POSITIVE, positive);
args.putInt(NEGATIVE, negative);
args.putParcelable(ARGS, argsIn);
frag.setArguments(args);
return frag;
}
public void onCancel(@NonNull DialogInterface dialog) {
final Bundle arguments = requireArguments();
runCallback(arguments.getInt(ID), true, arguments.getParcelable(ARGS));
super.onCancel(dialog);
}
private void runCallback(final int ID, boolean canceled, final Parcelable args) {
boolean handled = false;
if (getParentFragment() != null && getParentFragment() instanceof Callback)
handled = ((Callback) getParentFragment()).onAction(ID, canceled, args);
if (!handled && getActivity() instanceof Callback)
((Callback) getActivity()).onAction(ID, canceled, args);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Bundle arguments = requireArguments();
final int title = arguments.getInt(TITLE);
final int message = arguments.getInt(MESSAGE);
final int positive = arguments.getInt(POSITIVE);
final int negative = arguments.getInt(NEGATIVE);
return new AlertDialog.Builder(this.requireActivity())
.setTitle(title)
.setMessage(message)
.setPositiveButton(positive,
(dialog, whichButton) -> {
final Bundle arguments1 = requireArguments();
runCallback(arguments1.getInt(ID), false, arguments1.getParcelable(ARGS));
})
.setNegativeButton(negative, (dialog, whichButton) -> dialog.cancel()).create();
}
public interface Callback {
boolean onAction(final int ID, boolean canceled, final Parcelable args);
}
}
}
class SomeFragment extends Fragment implements GenericDialogFragment.PositiveNegative.Callback {
DELETE_ORDER = 666;
void usage() {
Parcelable someParcelableToGetTheContextOnCallback = null;
GenericDialogFragment.PositiveNegative.newInstance(DELETE_ORDER, R.string.some_title, R.string.some_message,
android.R.string.yes, android.R.string.no, someParcelableToGetTheContextOnCallback)
.show(getChildFragmentManager(), GenericDialogFragment.DIALOG_FRAGMENT_TAG)
}
@Override
public boolean onAction(int ID, boolean canceled, Parcelable args) {
if (ID == DELETE_ORDER) {
if (!canceled) {
//args is someParcelableToGetTheContextOnCallback
//do something on yes
}
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment