Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. import android.app.Activity;
  2. import android.app.Dialog;
  3. import android.support.v4.app.DialogFragment;
  4.  
  5. /**
  6. * Base dialog fragment.
  7. * @param <Listener> listener type.
  8. */
  9. public abstract class BaseDialogFragment<Listener> extends DialogFragment {
  10.  
  11. /** Dialog listener instance. */
  12. private Listener listener;
  13.  
  14. /**
  15. * Provides listener class to cast listener.
  16. * @return listener class.
  17. */
  18. protected Class<Listener> getListenerClass() {
  19. return null;
  20. }
  21.  
  22. public Listener getListener() {
  23. return listener;
  24. }
  25.  
  26. /**
  27. * Indicates whether listener is optional.
  28. * @return true if listener is optional.
  29. */
  30. protected boolean isListenerOptional() {
  31. return getListenerClass() == null;
  32. }
  33.  
  34. @SuppressWarnings("unchecked")
  35. @Override
  36. public void onAttach(Activity activity) {
  37. super.onAttach(activity);
  38. final Class<Listener> clazz = getListenerClass();
  39. if (clazz == null) {
  40. listener = null;
  41. } else if (getParentFragment() != null
  42. && clazz.isAssignableFrom(getParentFragment().getClass())) {
  43. listener = (Listener) getParentFragment();
  44. } else if (clazz.isAssignableFrom(activity.getClass())) {
  45. listener = (Listener) activity;
  46. } else {
  47. listener = null;
  48. }
  49. if (listener == null && !isListenerOptional()) {
  50. throw new ClassCastException(activity.getClass().getName() + " must implement listener");
  51. }
  52. }
  53.  
  54. public boolean isShowing() {
  55. final Dialog dialog = getDialog();
  56. return dialog != null && dialog.isShowing();
  57. }
  58.  
  59. // Hack for android issue 17423 in the compatibility library
  60. @Override
  61. public void onDestroyView() {
  62. if (getDialog() != null && getRetainInstance()) {
  63. getDialog().setDismissMessage(null);
  64. }
  65. super.onDestroyView();
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement