Repository: flutter
Issue: [Android][add2app] Cached engine is destroyed/detach after a few times navigating between Android and Flutter
Labels: c: new feature,platform-android,engine,a: existing-apps,has reproducible steps,P2,team-android,triaged-android,found in release: 3.16,found in release: 3.20

### Steps to reproduce

starting flutter activity with cached engine.
start native activity from flutter
start flutter activity from that native activity
start native activity from flutter
then back,back,back,back

### Expected results

expected that flutter engine survives activity.
checked FlutterActivity, it shouldn't be destroyed when cachedEngineId is set.
I also tried to set destroy_engine_with_activity false via bundle args without success.

### Actual results

the first time back to flutter works, the second time the engine is removed. Empty grey screen is shown, see error logs below.


### Code sample

```java
private void initFluter(){
    FlutterEngine flutterEngine = new FlutterEngine(getApplicationContext());
    flutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
    FlutterEngineCache.getInstance().put(FLUTTER_ENGINE_CACHE_KEY, flutterEngine);
}
```

```java
public void startFlutterActivity(String route) {
    FlutterEngineCache.getInstance().get(FLUTTER_ENGINE_CACHE_KEY).getNavigationChannel().pushRoute(route);
    startActivity(FlutterActivity.withCachedEngine(FLUTTER_ENGINE_CACHE_KEY).build(getActivity()));
}
```

the last backPress just calling ```getOnBackPressedDispatcher().onBackPressed()```



### Logs

<details open><summary>Logs</summary>

```console
FlutterActivity io.flutter.embedding.android.FlutterActivity@26a9582 connection to the engine io.flutter.embedding.engine.FlutterEngine@6048067 evicted by another attaching activity

FlutterActivity 227426304 onResume called after detach.
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console

[✓] Flutter (Channel stable, 3.16.9, on Ubuntu Noble Numbat (development branch) 6.6.0-14-generic, locale en_DE.UTF-8)
    • Flutter version 3.16.9 on channel stable at /home/bwahlen/snap/flutter/common/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 41456452f2 (8 days ago), 2024-01-25 10:06:23 -0800
    • Engine revision f40e976bed
    • Dart version 3.2.6
    • DevTools version 2.28.5

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /home/bwahlen/android-sdk-linux
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /home/bwahlen/android-sdk-linux
    • Java binary at: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java
    • Java version OpenJDK Runtime Environment Temurin-17.0.10+7 (build 17.0.10+7)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop
    • clang version 10.0.0-4ubuntu1
    • cmake version 3.16.3
    • ninja version 1.10.0
    • pkg-config version 0.29.1

[✓] Android Studio (version 2023.1)
    • Android Studio at /home/bwahlen/android-studio
    • Flutter plugin version 77.2.1
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version openjdk version "17.0.7" 2023-04-18

[✓] Connected device (3 available)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 14 (API 34) (emulator)
    • Linux (desktop)              • linux         • linux-x64      • Ubuntu Noble Numbat (development branch)
      6.6.0-14-generic
    • Chrome (web)                 • chrome        • web-javascript • Google Chrome 121.0.6167.139

[✓] Network resources
    • All expected network resources are available.

• No issues found!

```

</details>