Repository: flutter
Issue: [in_app_purchase][android] launchMode="singleInstance", opening Play Store bottom sheet, and navigating away from app and back kills the Flutter app
Labels: c: crash,platform-android,p: in_app_purchase,package,has reproducible steps,P2,found in release: 3.3,found in release: 3.7,team-android,triaged-android

## Steps to Reproduce
<!-- Please include full steps to reproduce so that we can reproduce the problem. -->
1. With any Flutter application using the `in_app_purchase` package, open your main / release AndroidManifest.xml file (by default in /android/app/src/main/)
2. Set `android:launchMode="singleInstance"` (either by changing the existing value for `launchMode` or adding that line if you don't have it.
3. Execute `flutter run --release` while a physical Android device is plugged in (Note: this also happens on versions of apps installed from the Play Store if you build the app and upload it there)
4. Navigate to wherever you can make purchases in your app, and tap to make a purchase. It does not matter what type of purchase it is - subscription, consumable, etc. This should open the Play Store bottom sheet.
5. Navigate away from your app by hitting the home button or recents button or by swiping (whichever you have enabled).
6. Navigate back to your app from the recents list*. The Play Store bottom sheet should be visible, but your app will not be visible underneath it. If you either continue making a purchase or dismiss the bottom sheet, trying to open the app again from your recent apps won't work. You'll have to swipe it away from your recent apps and relaunch it.

*If you navigate back to your app by tapping on its icon from your list of installed apps, it'll open your Flutter app again as if the Play Store bottom sheet wasn't opened. It's almost like the Play Store bottom sheet is switched to another Activity than the one the Flutter app is running on.

**Expected results:** <!-- what did you want to see? -->
Having a Play Store bottom sheet open, navigating away from the app, and navigating back to the app not having any impact on the app while `launchMode="singleInstance"`

**Actual results:** <!-- what did you see? -->
Having a Play Store bottom sheet open, navigating away from the app, and navigating back to the app from recents appears to replace the Activity that the Flutter app is running from with a new Activity that is only running the Play Store's bottom sheet. All traces of Flutter seem to just get detached or killed without respecting the Flutter lifecycle. This can also affect the next launch of the app, because the app got killed while a purchase was pending. If the user navigates back to the app while it's in the error state and make a purchase, further attempts to make a purchase might run into an IAPError with message "BillingResponse.itemAlreadyOwned". Restoring purchases will work for subscriptions if the user subscribes in that Play Store bottom sheet only screen, but hitting restore purchases for consumables seems like it gives a new consumable every time the restore purchase button is tapped, regardless of how many were purchased. Trying to buy a consumable in that unlimited-restore-purchase state runs into the "itemAlreadyOwned" message. That might not happen in an app that verifies purchases with the app's server though - I only tested consumables with the example app.

I'm not going to attach any code samples because of how much setup an app requires to be ready for in-app purchases. If you just add that `android:launchMode="singleInstance"` line to any project that is already set up for in-app purchases, you should be able to run into this issue.

<details>
  <summary>Logs</summary>
<!--
     Run `flutter analyze` and attach any output of that command below.
     If there are any analysis errors, try resolving them before filing this issue.
-->

```
Analyzing example...                                                    
No issues found! (ran in 1.6s)
```

<!-- Finally, paste the output of running `flutter doctor -v` here. -->

```
[✓] Flutter (Channel stable, 3.3.10, on macOS 12.6 21G115 darwin-arm, locale en-US)
    • Flutter version 3.3.10 on channel stable at /Users/matthewsisinni/Documents/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 135454af32 (5 weeks ago), 2022-12-15 07:36:55 -0800
    • Engine revision 3316dd8728
    • Dart version 2.18.6
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0-rc1)
    • Android SDK at /Users/matthewsisinni/Library/Android/sdk
    • Platform android-33, build-tools 32.0.0-rc1
    • ANDROID_HOME = /Users/matthewsisinni/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14A400
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.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 11.0.12+0-b1504.28-7817840)

[✓] IntelliJ IDEA Ultimate Edition (version 2022.1)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin version 221.5588

[✓] Connected device (3 available)
    • Pixel 6 Pro (mobile) • 1A271FDEE007AT • android-arm64  • Android 13 (API 33)
    • macOS (desktop)      • macos          • darwin-arm64   • macOS 12.6 21G115 darwin-arm
    • Chrome (web)         • chrome         • web-javascript • Google Chrome 109.0.5414.87

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
```

</details>


<details>
  <summary>Screenshots</summary>

This shows the example app from the in_app_purchases repo, configured for my developer account.
![Screenshot_20230118-172855](https://user-images.githubusercontent.com/6568129/213310317-4392a726-dee4-4516-87d7-2fc5687eba80.png)
Opening the Play Store bottom sheet by hitting the Silver option's button
![Screenshot_20230118-172908](https://user-images.githubusercontent.com/6568129/213310407-086afa7b-68c5-4043-bbfd-fdeb93210274.png)
Hitting the recents button before then hitting the home button
![Screenshot_20230118-172951](https://user-images.githubusercontent.com/6568129/213310418-45247678-59c5-4037-88b0-8be49279ff13.png)
Hitting the recents button after having gone to the home screen. Note that it stopped showing the Flutter app in the background and is instead showing my home screen behind the Play Store bottom sheet.
![Screenshot_20230118-172958](https://user-images.githubusercontent.com/6568129/213310427-53829754-c97a-41e6-bd7a-bcc7e7cef9b5.png)
Dismissing the Play Store bottom sheet and hitting the recents button again. Tapping on the app does not open it back up or close recents.
![Screenshot_20230118-173021](https://user-images.githubusercontent.com/6568129/213310482-8dff9e56-9901-45ea-b0de-4d143527567d.png)
Image showing what happens if you go through all those steps, but, instead of dismissing the bottom sheet, you subscribe to the app, clear the app from recents (because it can't be opened), relaunch it, and attempt to buy the same product. Note that the previous images all involved the "Silver" option. This image focuses on going through the whole process with the "Gold" one. It doesn't make a difference which one is used.
![Screenshot_20230118-173126](https://user-images.githubusercontent.com/6568129/213310491-52fe0b1d-6487-4c89-962a-b47c675a45d8.png)

</details>

thanks to @eliasteeny for figuring out it was `singleInstance`