Last active
August 29, 2015 14:02
-
-
Save dmytrodanylyk/a53d0d484296c63ab886 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Android Fragment Dialog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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