Android ActivityControlSurface attachToActivity signature change

Summary

提示

If you use standard Android embedding Java classes like

FlutterActivity or FlutterFragment,

and don’t manually embed a FlutterView

inside your own custom Activity (this should be uncommon),

you can stop reading.

void attachToActivity(

@NonNull ExclusiveAppComponent exclusiveActivity,

@NonNull Lifecycle lifecycle);

is replacing the now deprecated method:

void attachToActivity(@NonNull Activity activity, @NonNull Lifecycle lifecycle);

The existing deprecated method with the Activity

parameter was removed in Flutter 2.

Context

In order for custom Activities to also supply the Activity

lifecycle events Flutter plugins expect using the

ActivityAware interface, the FlutterEngine

exposed a getActivityControlSurface() API.

This allows custom Activities to signal to the engine

(with which it has a (0|1):1 relationship) that

it was being attached or detached from the engine.

提示

This lifecycle signaling is done automatically when you

use the engine’s bundled FlutterActivity

or FlutterFragment, which should be the most

common case.

However, the previous API had the flaw that it didn’t

enforce exclusion between activities connecting to

the engine, thus enabling n:1 relationships between

the activity and the engine,

causing lifecycle cross-talk issues.

Description of change

After Issue #21272, instead of attaching your activity

to the FlutterEngine by using the:

void attachToActivity(@NonNull Activity activity, @NonNull Lifecycle lifecycle);

API, which is now deprecated, instead use:

void attachToActivity(

@NonNull ExclusiveAppComponent exclusiveActivity,

@NonNull Lifecycle lifecycle);

An ExclusiveAppComponent interface

is now expected instead of an Activity.

The ExclusiveAppComponent provides a callback

in case your exclusive activity is being replaced by

another activity attaching itself to the FlutterEngine.

The

void detachFromActivity();

API remains unchanged and you’re still expected

to call it when your custom

activity is being destroyed naturally.

Migration guide

If you have your own activity holding a

FlutterView, replace calls to:

void attachToActivity(@NonNull Activity activity, @NonNull Lifecycle lifecycle);

with calls to:

void attachToActivity(

@NonNull ExclusiveAppComponent exclusiveActivity,

@NonNull Lifecycle lifecycle);

Wrap your activity with an ExclusiveAppComponent

and implement the callback method:

void detachFromFlutterEngine();

to handle your activity being replaced by another

activity being attached to the FlutterEngine.

Generally, you want to perform the same detaching operations

as performed when the activity is being naturally destroyed.

Timeline

Landed in version: 1.23.0-7.0.pre

In stable release: 2.0.0

References

Motivating bug: Issue #66192—Non exclusive

UI components attached to the FlutterEngine causes

event crosstalk

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐