Repository: flutter
Issue: engine is unintentionally destroyed when testing multiple-engines
Labels: platform-android,engine,d: api docs,has reproducible steps,P2,found in release: 2.2,found in release: 2.4,team-android,triaged-android

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a bug or if our documentation doesn't have an answer
     to what you're looking for, then fill out the template below. Please read
     our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

## Steps to Reproduce

<!-- You must include full steps to reproduce so that we can reproduce the problem. -->
1. Clone https://github.com/flutter/samples
2. Edit project: `add_to_app/multiple_flutters/multiple_flutters_android`
   apply these changes:

```diff
diff --git a/add_to_app/multiple_flutters/multiple_flutters_android/app/src/main/java/dev/flutter/multipleflutters/DoubleFlutterActivity.kt b/add_to_app/multiple_flutters/multiple_flutters_android/app/src/main/java/dev/flutter/multipleflutters/DoubleFlutterActivity.kt
index a1ef4b7..093116b 100644
--- a/add_to_app/multiple_flutters/multiple_flutters_android/app/src/main/java/dev/flutter/multipleflutters/DoubleFlutterActivity.kt
+++ b/add_to_app/multiple_flutters/multiple_flutters_android/app/src/main/java/dev/flutter/multipleflutters/DoubleFlutterActivity.kt
@@ -85,7 +85,8 @@ class DoubleFlutterActivity : FragmentActivity(), EngineBindingsDelegate {
     }
 
     override fun onNext() {
-        val flutterIntent = Intent(this, MainActivity::class.java)
+        finish()
+        val flutterIntent = Intent(this, SingleFlutterActivity::class.java)
         startActivity(flutterIntent)
     }
 }
diff --git a/add_to_app/multiple_flutters/multiple_flutters_android/app/src/main/java/dev/flutter/multipleflutters/EngineBindings.kt b/add_to_app/multiple_flutters/multiple_flutters_android/app/src/main/java/dev/flutter/multipleflutters/EngineBindings.kt
index 6c0ac0d..0325c15 100644
--- a/add_to_app/multiple_flutters/multiple_flutters_android/app/src/main/java/dev/flutter/multipleflutters/EngineBindings.kt
+++ b/add_to_app/multiple_flutters/multiple_flutters_android/app/src/main/java/dev/flutter/multipleflutters/EngineBindings.kt
@@ -75,7 +75,7 @@ class EngineBindings(activity: Activity, delegate: EngineBindingsDelegate, entry
      */
     fun detach() {
         // TODO: Uncomment after https://github.com/flutter/engine/pull/24644 is on stable.
-        // engine.destroy();
+        engine.destroy();
         DataModel.instance.removeObserver(this)
         channel.setMethodCallHandler(null)
     }
diff --git a/add_to_app/multiple_flutters/multiple_flutters_android/app/src/main/java/dev/flutter/multipleflutters/SingleFlutterActivity.kt b/add_to_app/multiple_flutters/multiple_flutters_android/app/src/main/java/dev/flutter/multipleflutters/SingleFlutterActivity.kt
index 7316e53..54b9fde 100644
--- a/add_to_app/multiple_flutters/multiple_flutters_android/app/src/main/java/dev/flutter/multipleflutters/SingleFlutterActivity.kt
+++ b/add_to_app/multiple_flutters/multiple_flutters_android/app/src/main/java/dev/flutter/multipleflutters/SingleFlutterActivity.kt
@@ -31,7 +31,8 @@ class SingleFlutterActivity : FlutterActivity(), EngineBindingsDelegate {
     }
 
     override fun onNext() {
-        val flutterIntent = Intent(this, MainActivity::class.java)
+        finish()
+        val flutterIntent = Intent(this, DoubleFlutterActivity::class.java)
         startActivity(flutterIntent)
     }
 }
```
3. Run app with release: `./gradlew :app:installRelease`
4. Click Next (MainActivity)  -> Next (in SingleFlutterActivity)-> Next  (in DoubleFlutterActivity) , then the App crashes.

**Expected results:** SingleFlutterActivity started

**Actual results:** App crashing

<details>
  <summary>Logs</summary>

<!--
      Run your application with `flutter run --verbose` and attach all the
      log output below between the lines with the backticks. If there is an
      exception, please see if the error message includes enough information
      to explain how to solve the issue.
-->

```
2021-07-23 19:16:00.630 17199-17199/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2021-07-23 19:16:00.631 17199-17199/? A/DEBUG: Build fingerprint: 'HUAWEI/VOG-AL10/HWVOG:10/HUAWEIVOG-AL10/10.1.0.162C00:user/release-keys'
2021-07-23 19:16:00.631 17199-17199/? A/DEBUG: Revision: '0'
2021-07-23 19:16:00.631 17199-17199/? A/DEBUG: ABI: 'arm64'
2021-07-23 19:16:00.633 17199-17199/? A/DEBUG: SYSVMTYPE: Maple
    APPVMTYPE: Art
2021-07-23 19:16:00.633 17199-17199/? A/DEBUG: Timestamp: 2021-07-23 19:16:00+0800
2021-07-23 19:16:00.633 17199-17199/? A/DEBUG: pid: 16985, tid: 17167, name: 1.raster  >>> dev.flutter.multipleflutters <<<
2021-07-23 19:16:00.633 17199-17199/? A/DEBUG: uid: 10415
2021-07-23 19:16:00.633 17199-17199/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
2021-07-23 19:16:00.633 17199-17199/? A/DEBUG: Cause: null pointer dereference
2021-07-23 19:16:00.633 17199-17199/? A/DEBUG:     x0  0000000000000000  x1  0000006fb77fb618  x2  0000006fa6a00000  x3  0000000000000005
2021-07-23 19:16:00.633 17199-17199/? A/DEBUG:     x4  0000000000000046  x5  0000000000000008  x6  00000070af846000  x7  000000002c888cc0
2021-07-23 19:16:00.634 17199-17199/? A/DEBUG:     x8  0000006fa726e1c0  x9  0000006fa726e1c8  x10 0000000000000003  x11 0000000000000000
2021-07-23 19:16:00.634 17199-17199/? A/DEBUG:     x12 0000000000000033  x13 0000000000000050  x14 ffffffffffffffb0  x15 0000006fa6b90000
2021-07-23 19:16:00.634 17199-17199/? A/DEBUG:     x16 0000006fb7cd0a70  x17 00000070aa099668  x18 0000006fc9bf0000  x19 0000006fb3b39770
2021-07-23 19:16:00.634 17199-17199/? A/DEBUG:     x20 000000000000006d  x21 0000007028a6f208  x22 00000070179d4180  x23 0000006fa6b90000
2021-07-23 19:16:00.634 17199-17199/? A/DEBUG:     x24 0000006fa6ae95f0  x25 00056cef37521c39  x26 0000006fa6b90050  x27 0000006fa6ae95f0
2021-07-23 19:16:00.634 17199-17199/? A/DEBUG:     x28 0000006fb7da2000  x29 00000070179d4160
2021-07-23 19:16:00.634 17199-17199/? A/DEBUG:     sp  0000007017df57f0  lr  0000006fb77fb63c  pc  0000006fb77fb644
2021-07-23 19:16:00.676 17199-17199/? A/DEBUG: backtrace:
2021-07-23 19:16:00.677 17199-17199/? A/DEBUG:       #00 pc 00000000002f2644  /data/app/dev.flutter.multipleflutters-GKgHWx-vVm7rUkFkoAwSmA==/base.apk!libflutter.so (offset 0x67c000) (BuildId: bcc3e4933283aef579232cc817456ca41bc3acaa)
2021-07-23 19:16:00.677 17199-17199/? A/DEBUG:       #01 pc 0000000000046bcc  [anon:libc_malloc]
```

<!--
     Run `flutter analyze` and attach any output of that command below.
     If there are any analysis errors, try resolving them before filing this issue.
-->

```
```

<!-- Finally, paste the output of running `flutter doctor -v` here. -->

```
[✓] Flutter (Channel stable, 2.2.3, on Mac OS X 10.15.7 19H1217 darwin-x64, locale zh-Hans-CN)
    • Flutter version 2.2.3 at /Users/wangyongrong/Library/flutter
    • Framework revision f4abaa0735 (3 weeks ago), 2021-07-01 12:46:11 -0700
    • Engine revision 241c87ad80
    • Dart version 2.13.4
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at /Users/wangyongrong/Documents/Android-SDK
    • Platform android-30, build-tools 30.0.2
    • ANDROID_HOME = /Users/wangyongrong/Documents/Android-SDK
    • Java binary at: /Applications/Android Studio 2.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.2, Build version 12B45b
    • CocoaPods version 1.10.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.1)
    • Android Studio at /Applications/Android Studio 2.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 1.8.0_242-release-1644-b3-6915495)

[✓] VS Code (version 1.58.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.24.0

[✓] Connected device (3 available)
    • VOG AL10 (mobile) • APH7N19329001298 • android-arm64  • Android 10 (API 29)
    • macOS (desktop)   • macos            • darwin-x64     • Mac OS X 10.15.7 19H1217 darwin-x64
    • Chrome (web)      • chrome           • web-javascript • Google Chrome 91.0.4472.164
```

</details>