Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 866
Joel Robinson-Johnson Avatar asked Mar 06 '18 00:03

Joel Robinson-Johnson


1 Answers

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.

like image 67
Vivart Avatar answered Oct 10 '22 07:10

Vivart