0

What's the proper way to start a Litho animation when an Activity is first displayed. All of the Litho animation examples are initiated by a user action, but I want to run one automatically.

I tried to extend a Litho animation example RTAnimationComponentSpec to fire the animation for the @OnEvent(VisibleEvent.class) instead of just @OnEvent(ClickEvent.class). But it didn't fire though.

Existing click event handler:

  @OnEvent(ClickEvent.class)
  static void onClick(ComponentContext c) {
    RTAnimationComponent.updateStateSync(c);
  }

Additional event handler that I added:

  @OnEvent(VisibleEvent.class)
  static void onVisible(ComponentContext c) {
    RTAnimationComponent.updateStateSync(c);
  }

I confirmed the VisibleEvent didn't fire by:

  1. Loading the Render Thread example and confirmed the animation didn't start
  2. Setting a breakpoint in the onVisible() method

How can I run a Litho animation automatically?

Michael Osofsky
  • 11,429
  • 16
  • 68
  • 113
  • You likely didn't add the visibility event to a component. Add `.visibleHandler(ContinuousExampleComponent.onVisible(c))` to your root component in `@OnCreateLayout` – Andy Oct 08 '21 at 08:45

1 Answers1

1

One solution I found works is to make use of the @OnCreateInitialState

@OnCreateInitialState
static void createInitialState(
        ComponentContext c,
        StateValue<Boolean> state) {
    state.set(true);
}

This runs the animation, but I'm not sure if it's the preferred way.

Michael Osofsky
  • 11,429
  • 16
  • 68
  • 113