nyarian
nyarian

Reputation: 4375

Flutter plugin: detect orientation change in native (android) side

I am writing a platform view backed plugin for Flutter which uses OpenGL during rendering. When orientation changes I need to update EGL context to reference actual window-backed rendering context.

But how can I detect orientation change (e.g. in PlatformView subtype)?

Implemented Application.ActivityLifecycleCallbacks methods do not seem to get invoked at all on rotation after attaching then to the Application instance.

Upvotes: 1

Views: 460

Answers (1)

salihgueler
salihgueler

Reputation: 3632

The reason that you are not getting a callback for the orientation change with the activity lifecycle callbacks is, in your manifest file, you can see

android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"

And this prevents your activity to be recreated on orientation changes with the native Android lifecycle. What you should be doing is, to use OrientationBuilder

e.g.

OrientationBuilder(
  builder: (context, orientation) {
    return GridView.count(
      // Create a grid with 2 columns in portrait mode,
      // or 3 columns in landscape mode.
      crossAxisCount: orientation == Orientation.portrait ? 2 : 3,
    );
  },
);

For more information please check out https://flutter.dev/docs/cookbook/design/orientation

Upvotes: 3

Related Questions