@Retention(value=RUNTIME) public @interface OnCreateInitialState
LayoutSpec
; the framework calls the method annotated with
OnCreateInitialState
before resolving its layout, i.e. before calling OnCreateLayout
. This lifecycle method should be used to set the initial values of the state of
the component. The annotate method can receive StateValue
containers for arguments
annotated with State
.
The framework can call OnCreateInitialState
from any thread. The method is invoked
only once during the lifecycle of the Component inside a ComponentTree - when it is first added
to the layout hierarchy. Any updates, such as state updates or prop changes, will not invoke this
method again if its global key did not change. Removing a component and adding it back to the
hierarchy will invoke this method again.
Required:
ComponentContext
Optional annotated arguments:
Prop
TreeProp
InjectProp
StateValue
@LayoutSpec
public class CounterSpec {
@OnCreateInitialState
static void onCreateInitialState(
ComponentContext c,
StateValue<Integer> count) {
count.set(0);
}
@OnCreateLayout
static Component onCreateLayout(
ComponentContext c,
@State int count) {
return Row.create(c)
.child(
Text.create(c)
.text(String.valueOf(count))
)
.build();
}
}