I'm having trouble discerning the difference between the LifeCycleRegistry
instance methods, handleLifecycleEvent
and markState
. According to the documentation handleLifecycleEvent
sets the current state and notifies the observers. markState
, on the other hand, moves the Lifecycle to the given state and dispatches necessary events to the observers.
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 i.e. Event and State. So that makes sense that Android provided two methods one for setting Event and 2nd for setting 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. e.g. from SupportActivity class
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With