[英]what's the difference between handleLifecycleEvent and markState (from the LifecycleRegistry class)
I'm having trouble discerning the difference between the LifeCycleRegistry
instance methods, handleLifecycleEvent
and markState
. 我无法辨别LifeCycleRegistry
实例方法, handleLifecycleEvent
和markState
之间的markState
。 According to the documentation handleLifecycleEvent
sets the current state and notifies the observers. 根据文档, handleLifecycleEvent
设置当前状态并通知观察者。 markState
, on the other hand, moves the Lifecycle to the given state and dispatches necessary events to the observers. 另一方面, markState
将生命周期移动到给定状态,并将必要的事件发送给观察者。
So, in both methods change the state and notifies observers so that they can fire the appropriate callbacks (based on my current understanding). 因此,在两种方法中都会更改状态并通知观察者,以便他们可以触发相应的回调(基于我当前的理解)。 Is there a case where these two methods aren't the same thing? 是否存在这两种方法不一样的情况?
Lifecycle uses two enums for lifecycle tracking ie Event and State. 生命周期使用两个枚举进行生命周期跟踪,即事件和状态。 So that makes sense that Android provided two methods one for setting Event and 2nd for setting State. 因此,Android提供了两种方法,一种用于设置Event,另一种用于设置State,这是有道理的。 If we see the code, both are doing almost same thing setting the state. 如果我们看到代码,两者都在设置状态几乎相同。
public void markState(@NonNull State state) {
moveToState(state);
}
public void handleLifecycleEvent(@NonNull Lifecycle.Event event) {
State next = getStateAfter(event);
moveToState(next);
}
but for difference I think markState makes more sense when you don't have exact event to match. 但是对于差异,我认为当你没有匹配的确切事件时,markState更有意义。 eg from SupportActivity class 例如来自SupportActivity类
protected void onSaveInstanceState(Bundle outState) {
mLifecycleRegistry.markState(Lifecycle.State.CREATED);
super.onSaveInstanceState(outState);
}
here we don't have any event corresponding to onSaveInstanceState so here markState makes more sense. 这里我们没有任何与onSaveInstanceState相对应的事件,所以这里markState更有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.