@Retention(value=RUNTIME) public @interface FromPreviousCreateLayout
OnCreateLayoutWithSizeSpec
lifecycle method. The name and type of the argument must match with
the inter-stage output requested. This inter-stage output is different from the other because
this OnShouldCreateLayoutWithNewSizeSpec
is called in subsequent layout calculations when
the framework has a cached layout to reuse.
This can be used in:
OnShouldCreateLayoutWithNewSizeSpec
The annotation processor will validate this and other invariants in the API at build time.
For example:
@LayoutSpec
class MyComponentSpec {
@OnCreateLayoutWithSizeSpec
static Component onCreateLayoutWithSizeSpec(
ComponentContext c,
int widthSpec,
int heightSpec) {
final Component textComponent =
Text.create(c).textSizeSp(16).text("Some text to measure.").build();
// UNSPECIFIED sizeSpecs will measure the text as being one line only,
// having unlimited width.
final Size textOutputSize = new Size();
textComponent.measure(
c,
SizeSpec.makeSizeSpec(0, UNSPECIFIED),
SizeSpec.makeSizeSpec(0, UNSPECIFIED),
textOutputSize);
// Small component to use in case textComponent does not fit within the current layout.
final Component imageComponent = Image.create(c).drawableRes(R.drawable.ic_launcher).build();
// Assuming SizeSpec.getMode(widthSpec) == EXACTLY or AT_MOST.
final int layoutWidth = SizeSpec.getSize(widthSpec);
final boolean textFits = (textOutputSize.width <= layoutWidth);
return textFits ? textComponent : imageComponent;
}
@OnShouldCreateLayoutWithNewSizeSpec
static boolean onShouldCreateLayoutWithNewSizeSpec(
ComponentContext c,
int newWidthSpec,
int newHeightSpec,
@FromPreviousCreateLayout int textWidth, // Get the output value
@FromPreviousCreateLayout boolean didItFit) {
final int newLayoutWidth = SizeSpec.getSize(newWidthSpec);
final boolean doesItStillFit = (textWidth <= newLayoutWidth);
// false if it still fits or if still doesn't fit
return doesItStillFit ^ didItFit;
}
}