Skip to content

Instantly share code, notes, and snippets.

@dmytrodanylyk
Last active August 29, 2015 14:02
Embed
What would you like to do?
Android Fragment Dialog
public class BaseDialogFragment extends DialogFragment {
public void showAllowStateLoss(@NotNull FragmentManager manager, @Nullable String tag) {
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(this, tag);
fragmentTransaction.commitAllowingStateLoss();
}
public void showAllowStateLoss(@NotNull FragmentManager manager) {
showAllowStateLoss(manager, null);
}
}
public class AlertFragmentDialog extends BaseDialogFragment {
public static final int STATE_OK_CANCEL = 0;
public static final int STATE_YES_NO = 1;
public static final int STATE_OK = 2;
public static final int DEFAULT_ID = -1;
private static final String EXTRAS_TITLE = "EXTRAS_TITLE";
private static final String EXTRAS_MESSAGE = "EXTRAS_MESSAGE";
private static final String EXTRAS_STATE = "EXTRAS_STATE";
private static final String EXTRAS_ID = "EXTRAS_ID";
public static BaseFragmentDialog newInstance(String title, String message, int state, int id) {
BaseFragmentDialog dialog = new BaseFragmentDialog();
Bundle bundle = new Bundle();
bundle.putString(EXTRAS_TITLE, title);
bundle.putString(EXTRAS_MESSAGE, message);
bundle.putInt(EXTRAS_STATE, state);
bundle.putInt(EXTRAS_ID, id);
dialog.setArguments(bundle);
return dialog;
}
public static BaseFragmentDialog newInstance(String title, String message, int state) {
return newInstance(title, message, state, DEFAULT_ID);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
String positiveText = null;
String negativeText = null;
int state = getArguments().getInt(EXTRAS_STATE, STATE_OK_CANCEL);
switch (state) {
case STATE_YES_NO:
positiveText = getString(R.string.Yes);
negativeText = getString(R.string.No);
break;
case STATE_OK_CANCEL:
positiveText = getString(android.R.string.ok);
negativeText = getString(android.R.string.cancel);
break;
case STATE_OK:
positiveText = getString(android.R.string.ok);
break;
}
dialog.setTitle(getArguments().getString(EXTRAS_TITLE));
dialog.setMessage(getArguments().getString(EXTRAS_MESSAGE));
setPositiveListener(dialog, positiveText);
if (negativeText != null) {
setNegativeListener(dialog, negativeText);
}
setCancelable(false);
return dialog.create();
}
private void setPositiveListener(AlertDialog.Builder dialog, CharSequence positive) {
final int id = getArguments().getInt(EXTRAS_ID, DEFAULT_ID);
dialog.setPositiveButton(positive, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (canTriggerActivityListener()) {
triggerPositiveActivityListener(id);
}
}
});
}
private void setNegativeListener(AlertDialog.Builder dialog, CharSequence negative) {
final int id = getArguments().getInt(EXTRAS_ID, DEFAULT_ID);
dialog.setNegativeButton(negative, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if (canTriggerActivityListener()) {
triggerNegativeActivityListener(id);
}
}
});
}
private boolean canTriggerActivityListener() {
return getActivity() instanceof AlertDialogListener;
}
private void triggerPositiveActivityListener(int dialogId) {
Activity activity = getActivity();
AlertDialogListener listener = (AlertDialogListener) activity;
listener.onPositiveButtonClicked(dialogId);
}
private void triggerNegativeActivityListener(int dialogId) {
Activity activity = getActivity();
AlertDialogListener listener = (AlertDialogListener) activity;
listener.onNegativeButtonClicked(dialogId);
}
public interface AlertDialogListener {
public void onPositiveButtonClicked(int dialogId);
public void onNegativeButtonClicked(int dialogId);
}
}
public final class DialogFactory {
private static final String TAG_LOADING_DIALOG = "TAG_LOADING_DIALOG";
public static void showLoadingDialog(@Nullable FragmentManager manager) {
if(manager != null) {
Fragment fragment = manager.findFragmentByTag(TAG_LOADING_DIALOG);
if (fragment == null) {
LoadingDialogFragment.newInstance().show(manager, TAG_LOADING_DIALOG);
}
}
}
public static void closeLoadingDialog(@Nullable FragmentManager manager) {
if(manager != null) {
Fragment fragment = manager.findFragmentByTag(TAG_LOADING_DIALOG);
if (fragment != null) {
LoadingDialogFragment dialogFragment = (LoadingDialogFragment) fragment;
dialogFragment.dismiss();
}
}
}
}
public class LoadingFragmentDialog extends BaseDialogFragment {
private static final String EXTRAS_TITLE = "EXTRAS_TITLE";
private static final String EXTRAS_MESSAGE = "EXTRAS_MESSAGE";
public static LoadingFragmentDialog newInstance(String title, String message) {
LoadingFragmentDialog dialog = new LoadingFragmentDialog();
Bundle bundle = new Bundle();
bundle.putString(EXTRAS_TITLE, title);
bundle.putString(EXTRAS_MESSAGE, message);
dialog.setArguments(bundle);
return dialog;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
ProgressDialog dialog = new ProgressDialog(getActivity());
dialog.setTitle(getArguments().getString(EXTRAS_TITLE));
dialog.setMessage(getArguments().getString(EXTRAS_MESSAGE));
setCancelable(false);
return dialog;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment