简体   繁体   English

handleLifecycleEvent和markState之间的区别是什么(来自LifecycleRegistry类)

[英]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实例方法, handleLifecycleEventmarkState之间的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.

相关问题 LifecycleRegistry 的 handleLifecycleEvent 是否给我错误的事件回调? 什么改变了? - Is LifecycleRegistry's handleLifecycleEvent giving me the wrong event callback? What changed? Class &lt;*&gt;和Class有什么区别<T> - What's the difference between Class<*> and Class<T> Android的DisplayMetrics类中的scaledDensity和density有什么区别? - what is the difference between scaledDensity and density in Android`s DisplayMetrics class? 这两种类初始化方法有什么区别? - What's the difference between these two approaches to class initialization? 在匕首模块的上下文中,Kotlin object 和 class 有什么区别 - What's the difference between Kotlin object and class in the context of a dagger module -keep类com.sample.Foo {*;}和-keep类com.sample.Foo有什么区别? - What's the difference between -keep class com.sample.Foo {*;} and -keep class com.sample.Foo? 用这两种方式指定一个类有什么区别? - What is the difference between specifying a class in these two ways? Flutter 中的 initState 和类构造函数有什么区别? - What is the difference between initState and a class constructor in Flutter? ViewModelProviders 和 ViewModelProvider 类有什么区别? - What is the difference between ViewModelProviders and ViewModelProvider class? BodyMimePart类中的setText()和setContent()有什么区别 - What is difference between setText() and setContent() in BodyMimePart class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM