1
0

DialogLifecycleCallback.java 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package com.kongzue.dialogx.interfaces;
  2. import androidx.annotation.NonNull;
  3. import androidx.lifecycle.Lifecycle;
  4. import androidx.lifecycle.LifecycleOwner;
  5. import androidx.lifecycle.LifecycleRegistry;
  6. import com.kongzue.dialogx.DialogX;
  7. /**
  8. * @author: Kongzue
  9. * @github: https://github.com/kongzue/
  10. * @homepage: http://kongzue.com/
  11. * @mail: myzcxhh@live.cn
  12. * @createTime: 2020/9/22 14:09
  13. */
  14. public abstract class DialogLifecycleCallback<T extends BaseDialog> implements LifecycleOwner {
  15. private final LifecycleRegistry registry = new LifecycleRegistry(this);
  16. public void onShow(T dialog) {
  17. try {
  18. //概率性报 no event down from INITIALIZED,目前尚不清楚为何
  19. if (registry.getCurrentState() != Lifecycle.State.CREATED) {
  20. registry.setCurrentState(Lifecycle.State.CREATED);
  21. }
  22. } catch (Exception e) {
  23. }
  24. if (DialogX.dialogLifeCycleListener != null && DialogX.dialogLifeCycleListener != this) {
  25. DialogX.dialogLifeCycleListener.onShow(dialog);
  26. }
  27. }
  28. public void onDismiss(T dialog) {
  29. try {
  30. if (registry.getCurrentState() != Lifecycle.State.DESTROYED) {
  31. //概率性报 no event down from INITIALIZED,目前尚不清楚为何
  32. registry.setCurrentState(Lifecycle.State.DESTROYED);
  33. }
  34. } catch (Exception e) {
  35. }
  36. if (DialogX.dialogLifeCycleListener != null && DialogX.dialogLifeCycleListener != this) {
  37. DialogX.dialogLifeCycleListener.onDismiss(dialog);
  38. }
  39. }
  40. @NonNull
  41. @Override
  42. public Lifecycle getLifecycle() {
  43. return registry;
  44. }
  45. }