Repository: flutter
Issue: Activity leaks when using Flutter Fragment in an Android app
Labels: platform-android,engine,c: performance,a: existing-apps,perf: memory,has reproducible steps,P2,team-android,triaged-android,found in release: 3.19,found in release: 3.21

### Steps to reproduce

1. Write android app (using Android Studio) and add to it flutter fragment to be shown in activity. Create your own FlutterEngine in Application extended class. Use this FlutterEngine to create fragment. Actually use steps exactly as in : https://docs.flutter.dev/add-to-app/android/add-flutter-fragment. 
2. After starting app, activity with flutter fragment is shown, then close activity by pressing back button (Activity.onDestroy is verified to be called)
3. Start in Android Studio profiler, and make a memory heap dump, 3 component leaks are shown
<img width="1155" alt="Screenshot 2024-03-26 at 12 26 05" src="https://github.com/flutter/flutter/assets/1464881/99d3bcde-131d-464d-8f16-0db6cadb2ed4">


### Expected results

No activity leaks

### Actual results

The problem is only with manually created FlutterEngine, if I create flutter fragment using FlutterFragment.createDefault() then no leaks appear.

In my test app the relevant code is in those files:

here I create engine:

https://github.com/luskan/FlutterEmbeddedTestApp/blob/main/FlutterEmbeddedTestApp/app/src/main/java/com/example/flutterembeddedtestapp/MyApplication.java

here is my activity:

https://github.com/luskan/FlutterEmbeddedTestApp/blob/main/FlutterEmbeddedTestApp/app/src/main/java/com/example/flutterembeddedtestapp/MainActivity.java

I made a custom engine and applied modification in android embedding code, which fixed those activity leaks:

https://github.com/luskan/engine/commit/26d743a34fae5a0ed26992b133e6861e36bc8e41



### Code sample

https://github.com/luskan/FlutterEmbeddedTestApp

### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

<details open><summary>Logs</summary>

```console
[Paste your logs here]
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
❯ flutter doctor -v
[✓] Flutter (Channel stable, 3.19.3, on macOS 14.4 23E214 darwin-x64, locale en-PL)
    • Flutter version 3.19.3 on channel stable at /Users/marcin/automapa/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ba39319843 (3 weeks ago), 2024-03-07 15:22:21 -0600
    • Engine revision 2e4ba9c6fb
    • Dart version 3.3.1
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/marcin/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C65
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.2)
    • 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 17.0.9+0-17.0.9b1087.7-11185874)

[✓] IntelliJ IDEA Ultimate Edition (version 2023.3.3)
    • IntelliJ at /Users/marcin/Applications/IntelliJ IDEA Ultimate.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin version 233.13763.5

[✓] Connected device (3 available)
    • SM S911B (mobile) • RFCW329115H • android-arm64  • Android 14 (API 34)
    • macOS (desktop)   • macos       • darwin-x64     • macOS 14.4 23E214 darwin-x64
    • Chrome (web)      • chrome      • web-javascript • Google Chrome 123.0.6312.59

[✓] Network resources
    • All expected network resources are available.

• No issues found!
```

</details>