Repository: flutter
Issue: [Android][Add-to-App]Crash on toggling system light/dark mode multiple times
Labels: platform-android,engine,dependency: dart,a: existing-apps,perf: memory,has reproducible steps,P2,c: fatal crash,found in release: 3.0,found in release: 3.1,team-android,triaged-android

## Steps to Reproduce

1. open app
2. switch to dark/light mode in status bar menu _(it doesn't seem to occur if go to settings and change it there)_

please check: https://drive.google.com/file/d/1w9_gcXE3tWb5_gTlZQBTm664xD7k79Px/view?usp=sharing

reproducible in a simple app with just an activity and flutter module _(seems to work without a flutter engine created)_. 

no error logs appear. 

sometimes it is quite difficult to reproduce, it might require switching the theme mode about 20 times, but in the prod app, with a few flutter fragments (that are added to other fragments) and few engines, it appears often,  2-5 times enough. 

while testing the prod app I saw a crash, but not sure if it is the same issue or another:

<details>
<summary> logs</summary>

```
io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onDestroyView FlutterActivityAndFragmentDelegate.java:613
io.flutter.embedding.android.FlutterFragment.detachFromFlutterEngine FlutterFragment.java:869
io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.detachFromFlutterEngine FlutterActivityAndFragmentDelegate.java:649
io.flutter.embedding.engine.FlutterEngineConnectionRegistry.attachToActivity FlutterEngineConnectionRegistry.java:319
io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onAttach FlutterActivityAndFragmentDelegate.java:189
io.flutter.embedding.android.FlutterFragment.onAttach FlutterFragment.java:777
androidx.fragment.app.Fragment.performAttach Fragment.java:2954
androidx.fragment.app.FragmentStateManager.attach FragmentStateManager.java:463
androidx.fragment.app.FragmentStateManager.moveToExpectedState FragmentStateManager.java:254
androidx.fragment.app.FragmentStore.moveToExpectedState FragmentStore.java:113
androidx.fragment.app.FragmentManager.moveToState FragmentManager.java:1374
androidx.fragment.app.FragmentManager.dispatchStateChange FragmentManager.java:2841
androidx.fragment.app.FragmentManager.dispatchCreate FragmentManager.java:2773
androidx.fragment.app.Fragment.restoreChildFragmentState Fragment.java:1935
androidx.fragment.app.Fragment.onCreate Fragment.java:1911
androidx.fragment.app.Fragment.performCreate Fragment.java:2981
androidx.fragment.app.FragmentStateManager.create FragmentStateManager.java:474
androidx.fragment.app.FragmentStateManager.moveToExpectedState FragmentStateManager.java:257
androidx.fragment.app.FragmentStore.moveToExpectedState FragmentStore.java:113
androidx.fragment.app.FragmentManager.moveToState FragmentManager.java:1374
androidx.fragment.app.FragmentManager.dispatchStateChange FragmentManager.java:2841
androidx.fragment.app.FragmentManager.dispatchCreate FragmentManager.java:2773
androidx.fragment.app.Fragment.restoreChildFragmentState Fragment.java:1935
androidx.fragment.app.Fragment.onCreate Fragment.java:1911
androidx.navigation.fragment.NavHostFragment.onCreate NavHostFragment.java:169
androidx.fragment.app.Fragment.performCreate Fragment.java:2981
androidx.fragment.app.FragmentStateManager.create FragmentStateManager.java:474
androidx.fragment.app.FragmentStateManager.moveToExpectedState FragmentStateManager.java:257
androidx.fragment.app.FragmentStore.moveToExpectedState FragmentStore.java:113
androidx.fragment.app.FragmentManager.moveToState FragmentManager.java:1374
androidx.fragment.app.FragmentManager.dispatchStateChange FragmentManager.java:2841
androidx.fragment.app.FragmentManager.dispatchCreate FragmentManager.java:2773
androidx.fragment.app.Fragment.restoreChildFragmentState Fragment.java:1935
androidx.fragment.app.Fragment.onCreate Fragment.java:1911
BaseNavHostFragment.onCreate BaseNavHostFragment.java:51
androidx.fragment.app.Fragment.performCreate Fragment.java:2981
androidx.fragment.app.FragmentStateManager.create FragmentStateManager.java:474
androidx.fragment.app.FragmentStateManager.moveToExpectedState FragmentStateManager.java:257
androidx.fragment.app.FragmentStore.moveToExpectedState FragmentStore.java:113
androidx.fragment.app.FragmentManager.moveToState FragmentManager.java:1374
androidx.fragment.app.FragmentManager.dispatchStateChange FragmentManager.java:2841
androidx.fragment.app.FragmentManager.dispatchCreate FragmentManager.java:2773
androidx.fragment.app.FragmentController.dispatchCreate FragmentController.java:251
androidx.fragment.app.FragmentActivity.onCreate FragmentActivity.java:252
BaseActivity.onCreate BaseActivity.java:28
MainActivity.onCreate MainActivity.java:251
android.app.Activity.performCreate Activity.java:7802
```
</details>

tested on API 29, but reproducible on other emulators/devices as well. 

simple code sample: https://drive.google.com/file/d/1-pkxBUen7LNLzgmk6QGaEsp40mX5hO9e/view?usp=sharing

```
[✓] Flutter (Channel stable, 3.0.1, on macOS 12.2.1 21D62 darwin-arm, locale en-DE)
    • Flutter version 3.0.1 at /Users/siarheism/Documents/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision fb57da5f94 (6 weeks ago), 2022-05-19 15:50:29 -0700
    • Engine revision caaafc5604
    • Dart version 2.17.1
    • DevTools version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /Users/siarheism/Library/Android/sdk
    • Platform android-32, build-tools 32.1.0-rc1
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)
    • All Android licenses accepted.

```