@Retention(value=CLASS)
public @interface OnPrepare
MountSpec
can define a method annotated with OnPrepare
to run code that is more
heavy and cannot be done during OnMount
or OnBind
. The method is called once
before the layout calculation is performed, and the framework can invoke it either on the UI
thread or on a background thread.
The annotated method has a void return type and will be passed the following arguments when the framework invokes it:
Required:
Optional annotated arguments:
Prop
TreeProp
InjectProp
State
The annotation processor will validate this and other invariants in the API at build time.
OnPrepare-annotated methods can calculate values which are heavy to compute and
pass them as inter-stage props to other methods which are performance critical, such as OnMount
.
For example:
@MountSpec
class ExampleMountSpec {
@OnPrepare
static void onPrepare(
ComponentContext c,
@Prop String colorName,
Output<Integer> color) {
color.set(Color.parseColor(colorName));
}
@OnCreateMountContent
static ColorDrawable onCreateMountContent(
ComponentContext c) {
return new ColorDrawable();
}
@OnMount
static void onMount(
ComponentContext c,
ColorDrawable colorDrawable,
@FromPrepare int color){
colorDrawable.setColor(color);
}
}