Repository: flutter
Issue: Flutter background isolate does not run when the app is started from the IDE on MacOS and subsequently closed on an Android device
Labels: platform-android,tool,engine,platform-mac,dependency: dart,P2,team-android,triaged-android

## Summary
While working on a Flutter project, I have encountered a problem bench testing a background task on an Android device.  This issue is reproducible in both IntelliJ and Android Studio with the example app from the flutter_background_fetch package on two different MacOS development systems. After investigating as thoroughly as I can, my working hypothesis is that the MacOS version of the flutter-intellij plugin does not start a background isolate when the debugger is attached.

(Note: Initially posted at youtrack.jetbrains.com/issue/IDEA-292108, where it was suggested that flutter-intellij project is the best place to post this issue.)

## Steps to Reproduce

1. Download the flutter background fetch plugin (current version is 1.1.0):
```
git clone https://github.com/transistorsoft/flutter_background_fetch.git
```
2. Open the Flutter project in either the IntelliJ IDE or the Android Studio IDE.

3. Run `flutter pub get` to resolve dependencies.

4. Connect the Android device via USB cable.

5. Create a run configuration with the IDE, if necessary, for example/lib/main.dart.  With the Android device selected, start the app by tapping the IDE 'debug' button.

6. After the app starts on the target device, tap the App 'Overview' button (recently used apps) from the Android system navigation bar and close the BackgroundFetch Example app (swipe off screen) to terminate it.  The UI isolate is closed but the background isolate should continue to run.

7. From the built-in Terminal in the IDE or from a command window execute the script `./scripts/simulate-fetch` from the project directory.

Expected result:  The background task is triggered and the output from the script execution is:
```
Running job [FORCED]
```
Actual result:  The background task is NOT triggered and the output from the script execution is:
```
Could not find job 999 in package com.transistorsoft.flutter.backgroundfetch.backgroundfetchexample / user 0
```

## Version info

**Environment**

* Development Platform
```
MacBook Pro (15-inch 2017)
Processor 2.9 GHz Quad-Core Intel Core I7
Memory 16 GB
macOS Monterey Version 12.3.1
```
* IntelliJ IDE
```
IntelliJ IDEA 2021.3.3 (Ultimate Edition)
Build #IU-213.7172.25, built on March 15, 2022
Runtime version: 11.0.14.1+1-b1751.46 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 12.3.1
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M

Non-Bundled Plugins:
org.jetbrains.kotlin (213-1.6.20-release-275-IJ6777.52)
Dart (213.7306)
io.flutter (66.0.3)
```
* Android Studio IDE
```
Android Studio Bumblebee | 2021.1.1 Patch 2
Build #AI-211.7628.21.2111.8193401, built on February 17, 2022
Runtime version: 11.0.11+0-b60-7590822 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 12.3.1
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Non-Bundled Plugins:
org.jetbrains.kotlin (211-1.6.20-release-275-AS7442.40)
Dart (211.7811)
io.flutter (66.0.1)
```
* Target Device
```
Samsung Galaxy S8+
Android 9 (Pie, API 28)
```
* Flutter info (`flutter doctor -v`)
```
[✓] Flutter (Channel stable, 2.10.4, on macOS 12.3.1 21E258 darwin-x64, locale en-US)
    • Flutter version 2.10.4 at /Users/[username]/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision c860cba910 (3 weeks ago), 2022-03-25 00:23:12 -0500
    • Engine revision 57d3bac3dd
    • Dart version 2.16.2
    • DevTools version 2.9.2

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc2)
    • Android SDK at /Users/[username]/Library/Android/sdk
    • Platform android-32, build-tools 33.0.0-rc2
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.1)
    • 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.11+0-b60-7590822)

[✓] IntellJ IDEA Ultimate Edition (version 2021.3.3)

    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin version 66.0.3
    • Dart plugin version 213.7306

[✓] Connected device (1 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 100.0.4896.88

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
```

### Additional Information

- Tried a Samsung Galaxy S21 with Android 12. No difference in behavior.
- Tried a clean install with IntelliJ and Android Studio on a different MacBook. No difference in behavior.
- Tried several different versions of IntelliJ (2021.1.3, 2021.2.4, and 2022.1) all with the same result.
- From the context menu in IntelliJ, select Flutter -> 'Open Android module in Android Studio'.  Follow steps 5-7 above.  The expected result is produced.
- In the IDE Terminal or in a command window, from the project example directory, execute `flutter run --debug lib/main.dart`. Repeat steps 6-7 above.  The expected result is produced.
- Finally, on a Linux development platform with IntelliJ 2021.3.3, the test behaves as expected after the app is closed and the background task is triggered via the simulate-fetch script.

The IntelliJ logs shows a number of flags passed to the flutter command to start the app when the 'debug' or 'run' buttons in the IDE are pressed, including the --start-paused flag.  Once the IDE attaches its debugger to the app, all isolates are supposed to run.  Because of the observed behavior, my guess is that only the UI isolate is started and not the background isolate.  The trials above show that the background task works correctly when the app is started as an Android app or is started directly with a `flutter run` command.  The behavior when running the IDE on a Linux platform may provide additional clues since correct behavior is also observed there.