android.arch.lifecycle.Lifecycle.removeObserver()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(5.5k)|赞(0)|评价(0)|浏览(203)

本文整理了Java中android.arch.lifecycle.Lifecycle.removeObserver()方法的一些代码示例,展示了Lifecycle.removeObserver()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Lifecycle.removeObserver()方法的具体详情如下:
包路径:android.arch.lifecycle.Lifecycle
类名称:Lifecycle
方法名:removeObserver

Lifecycle.removeObserver介绍

[英]Removes the given observer from the observers list.

If this method is called while a state change is being dispatched,

  • If the given observer has not yet received that event, it will not receive it.
  • If the given observer has more than 1 method that observes the currently dispatched event and at least one of them received the event, all of them will receive the event and the removal will happen afterwards.
    [中]从“观察者”列表中删除给定的观察者。
    如果在分派状态更改时调用此方法,
    *如果给定的观察者尚未接收到该事件,它将不会接收该事件。
    *如果给定的观察者有多个方法来观察当前分派的事件,并且其中至少有一个接收到该事件,则所有观察者都将接收该事件,随后将进行删除。

代码示例

代码示例来源:origin: JessYanCoding/MVPArms

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
  void onDestroy(LifecycleOwner owner) {
    owner.getLifecycle().removeObserver(this);
  }
}

代码示例来源:origin: JessYanCoding/MVPArms

/**
 * 只有当 {@code mRootView} 不为 null, 并且 {@code mRootView} 实现了 {@link LifecycleOwner} 时, 此方法才会被调用
 * 所以当您想在 {@link Service} 以及一些自定义 {@link View} 或自定义类中使用 {@code Presenter} 时
 * 您也将不能继续使用 {@link OnLifecycleEvent} 绑定生命周期
 *
 * @param owner link {@link SupportActivity} and {@link Fragment}
 */
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
void onDestroy(LifecycleOwner owner) {
  /**
   * 注意, 如果在这里调用了 {@link #onDestroy()} 方法, 会出现某些地方引用 {@code mModel} 或 {@code mRootView} 为 null 的情况
   * 比如在 {@link RxLifecycle} 终止 {@link Observable} 时, 在 {@link io.reactivex.Observable#doFinally(Action)} 中却引用了 {@code mRootView} 做一些释放资源的操作, 此时会空指针
   * 或者如果你声明了多个 @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) 时在其他 @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
   * 中引用了 {@code mModel} 或 {@code mRootView} 也可能会出现此情况
   */
  owner.getLifecycle().removeObserver(this);
}

代码示例来源:origin: M66B/XPrivacyLua

void stopObserving() {
  if (this.owner != null && this.dialog != null) {
    owner.getLifecycle().removeObserver(this);
    this.dialog = null;
  }
}

代码示例来源:origin: SilenceDut/TaskScheduler

@Override
  public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
    if(event == targetEvent) {
      if(mLifecycleOwner!=null ) {
        mLifecycleOwner.getLifecycle().removeObserver(this);
      }
      handler.removeCallbacks(LifecycleRunnableDelegate.this);
    }
  }
};

代码示例来源:origin: SilenceDut/TaskScheduler

@Override
  public void run() {
    if(mOriginRunnable!=null && mLifecycleOwner!=null) {
      mOriginRunnable.run();
      mLifecycleOwner.getLifecycle().removeObserver(mLifecycleObserver);
    }

  }
}

代码示例来源:origin: XunMengWinter/Now

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
  public void onDisPose() {
//        Logger.t(TAG).i("dispose");
    if (mLifecycle != null)
      mLifecycle.removeObserver(this);
    if (mDisposable != null && !mDisposable.isDisposed())
      mDisposable.dispose();
  }

代码示例来源:origin: XunMengWinter/Now

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void destroy() {
  print();
  mLifecycle.removeObserver(this);
}

代码示例来源:origin: JessYanCoding/MVPArt

/**
 * 只有当 {@code view} 实现了 {@link LifecycleOwner} 时, 此方法才会被调用
 * 所以当您想在 {@link Service} 以及一些自定义 {@link View} 或自定义类中使用 {@code Presenter} 时
 * 您也将不能继续使用 {@link OnLifecycleEvent} 绑定生命周期
 *
 * @param owner link {@link SupportActivity} and {@link Fragment}
 */
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
void onDestroy(LifecycleOwner owner) {
  /**
   * 注意, 如果在这里调用了 {@link #onDestroy()} 方法, 会出现某些地方引用 {@code mModel} 为 null 的情况
   * 比如如果你声明了多个 @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) 时在其他 @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
   * 中引用了 {@code mModel} 也可能会出现此情况
   */
  owner.getLifecycle().removeObserver(this);
}

代码示例来源:origin: listenzz/AndroidNavigation

@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
void onStateChange() {
  if (getLifecycle().getCurrentState() == Lifecycle.State.DESTROYED) {
    tasks.clear();
    getLifecycle().removeObserver(this);
  } else {
    considerExecute();
  }
}

代码示例来源:origin: WaylonBrown/LifecycleAwareRx

@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
  void onStateChange() {
    if (lifecycleOwner != null && lifecycleOwner.getLifecycle().getCurrentState() == Lifecycle.State.DESTROYED) {
      // No memory leaks please
      lifecycleOwner.getLifecycle().removeObserver(this);
      lifecycleOwner = null;
    }
  }
}

代码示例来源:origin: smartdevicelink/sdl_android

@Override
public void dispose(){
  // send broadcast to close lock screen if open
  if (context.get() != null) {
    context.get().sendBroadcast(new Intent(SDLLockScreenActivity.CLOSE_LOCK_SCREEN_ACTION));
  }
  // remove listeners
  internalInterface.removeOnRPCNotificationListener(FunctionID.ON_HMI_STATUS, hmiListener);
  internalInterface.removeOnRPCNotificationListener(FunctionID.ON_DRIVER_DISTRACTION, ddListener);
  if (deviceLogoEnabled) {
    internalInterface.removeOnRPCNotificationListener(FunctionID.ON_SYSTEM_REQUEST, systemRequestListener);
  }
  deviceLogo = null;
  deviceIconUrl = null;
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
    try {
      if (android.arch.lifecycle.ProcessLifecycleOwner.get() != null && lifecycleObserver != null) {
        android.arch.lifecycle.ProcessLifecycleOwner.get().getLifecycle().removeObserver(lifecycleObserver);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    lifecycleObserver = null;
  }
  isApplicationForegrounded = false;
  super.dispose();
}

代码示例来源:origin: WaylonBrown/LifecycleAwareRx

/**
 * Decides whether the stream needs to be destroyed or subscribed to.
 */
private void handleCurrentLifecycleState() {
  if (lifecycleOwner != null &&
    lifecycleOwner.getLifecycle().getCurrentState() == Lifecycle.State.DESTROYED) {
    // No memory leaks please
    this.lifecycleOwner.getLifecycle().removeObserver(this);
    this.lifecycleOwner = null;
    this.baseReactiveType = null;
  } else if (LifecycleUtil.isInActiveState(lifecycleOwner) 
    && !subscribed 
    && baseReactiveType != null) {
    
    // Subscribe to stream with observer since the LifecycleOwner is now active but wasn't previously
    baseReactiveType.subscribeWithObserver();
    subscribed = true;
  }
}

相关文章

微信公众号

最新文章

更多