Skip to content

[shared engine][android] Unexpected behavior in detachFromFlutterEngine #94802

Closed
flutter-team-archive/engine
#30202

Description

@0xZOne

The behavior of the |detachFromFlutterEngine| in FlutterActivity/FlutterFragment is NOT as expected.

https://github.com/flutter/engine/blob/main/shell/platform/android/io/flutter/embedding/android/FlutterActivity.java#L640

截屏2021-12-07 下午8 39 05

Please consider the following scenario where multiple FlutterActivities share one FlutterEngine (E.g, flutter_boost):

  1. Open page A implemented by FlutterActivity A.
  2. Open page B implemented by FlutterActivity B from page A. (FlutterActivity A is detached from the engine.)
  3. Go back to the previous page A from page B. (FlutterActivity B is detached from the engine, while FlutterActivity A is reattached.)

Actual results:
As the picture shows when a new Activity is being attached while an existing Activity is already attached, the existing Activity is disposed immediately and can't be reused anymore.

Expected results:
In the scenario of shared-engine, we expect that the detached Activity can be re-attached later instead of being disposed.

flutter doctor -v
[✓] Flutter (Channel master, 2.6.0-12.0.pre.757, on macOS 12.0.1 21A559 darwin-x64, locale zh-Hans-CN)
    • Flutter version 2.6.0-12.0.pre.757 at /Users/zero/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 1ba81373ee (3 weeks ago), 2021-11-17 22:01:34 -0500
    • Engine revision a3ef1d5351
    • Dart version 2.16.0 (build 2.16.0-20.0.dev)
    • DevTools version 2.8.0

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/zero/Library/Android/sdk
    • Platform android-Sv2, build-tools 31.0.0
    • ANDROID_HOME = /Users/zero/Library/Android/sdk
    • ANDROID_SDK_ROOT = /Users/zero/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
    • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 12.5.1)
    • Xcode at /Applications/Xcode12.5.app/Contents/Developer
    ! Flutter recommends a minimum Xcode version of 13.
      Download the latest version or update via the Mac App Store.
    • CocoaPods version 1.10.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)

[✓] VS Code (version 1.62.3)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (2 available)
    • iPhone (mobile) • 50131fb4dcef9d96405ec953e272bd5c24468370 • ios            • iOS 15.0 19A346
    • Chrome (web)    • chrome                                   • web-javascript • Google Chrome 96.0.4664.55

! Doctor found issues in 1 category.

Activity

  1. changed the title [-][shared engine][android] The behavior of the detachFromFlutterEngine is not as expected[/-] [+][shared engine][android] Unexpected behavior in detachFromFlutterEngine[/+] on Dec 8, 2021
  2. 0xZOne commented on Dec 8, 2021

    @0xZOne
    MemberAuthor

    An alternative solution is for the developer to implement FlutterActivity/FlutterFragment subclasses to override |detachFromFlutterEngine|.

    Or In FlutterActivity/FlutterFragment subclass override |shouldAttachEngineToActivity| to return false, and call flutterEngine.getActivityControlSurface().attachToActivity/detachFromActivity at the right time and place by developer.

    But, the precondition is the subclass can access FlutterActivityAndFragmentDelegate (ExclusiveAppComponent<Activity>). (Related PR: flutter-team-archive/engine#30126)

  3. darshankawar commented on Dec 8, 2021

    @darshankawar
    Member
    1. Open page A implemented by FlutterActivity A.
    2. Open page B implemented by FlutterActivity B from page A. (FlutterActivity A is detached from the engine.)
    3. Go back to the previous page A from page B. (FlutterActivity B is detached from the engine, while FlutterActivity A is reattached.)

    @0xZOne
    Can you provide us a minimal code sample representing above scenarios so that we can verify it further ?
    Thanks.

  4. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Dec 8, 2021
  5. 0xZOne commented on Dec 8, 2021

    @0xZOne
    MemberAuthor
    1. Open page A implemented by FlutterActivity A.
    2. Open page B implemented by FlutterActivity B from page A. (FlutterActivity A is detached from the engine.)
    3. Go back to the previous page A from page B. (FlutterActivity B is detached from the engine, while FlutterActivity A is reattached.)

    @0xZOne Can you provide us a minimal code sample representing above scenarios so that we can verify it further ? Thanks.

    In fact, the conclusion can be simply obtained from the logic of the above code.

  6. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Dec 8, 2021
  7. blasten commented on Dec 8, 2021

    @blasten

    Yes, this seems incorrect. cc @gaaclarke

  8. added
    r: fixedIssue is closed as already fixed in a newer version
    on Dec 10, 2021
  9. github-actions commented on Dec 24, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  10. locked as resolved and limited conversation to collaborators on Dec 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    engineflutter/engine related. See also e: labels.platform-androidAndroid applications specificallyr: fixedIssue is closed as already fixed in a newer versionwaiting for PR to land (fixed)A fix is in flight

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions