Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.Activity;
- import android.app.Dialog;
- import android.support.v4.app.DialogFragment;
- /**
- * Base dialog fragment.
- * @param <Listener> listener type.
- */
- public abstract class BaseDialogFragment<Listener> extends DialogFragment {
- /** Dialog listener instance. */
- private Listener listener;
- /**
- * Provides listener class to cast listener.
- * @return listener class.
- */
- protected Class<Listener> getListenerClass() {
- return null;
- }
- public Listener getListener() {
- return listener;
- }
- /**
- * Indicates whether listener is optional.
- * @return true if listener is optional.
- */
- protected boolean isListenerOptional() {
- return getListenerClass() == null;
- }
- @SuppressWarnings("unchecked")
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- final Class<Listener> clazz = getListenerClass();
- if (clazz == null) {
- listener = null;
- } else if (getParentFragment() != null
- && clazz.isAssignableFrom(getParentFragment().getClass())) {
- listener = (Listener) getParentFragment();
- } else if (clazz.isAssignableFrom(activity.getClass())) {
- listener = (Listener) activity;
- } else {
- listener = null;
- }
- if (listener == null && !isListenerOptional()) {
- throw new ClassCastException(activity.getClass().getName() + " must implement listener");
- }
- }
- public boolean isShowing() {
- final Dialog dialog = getDialog();
- return dialog != null && dialog.isShowing();
- }
- // Hack for android issue 17423 in the compatibility library
- @Override
- public void onDestroyView() {
- if (getDialog() != null && getRetainInstance()) {
- getDialog().setDismissMessage(null);
- }
- super.onDestroyView();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement