Repository: flutter
Issue: [url_launcher] - PlatformException when launching URLs from a Flutter AAR module added to an Android app
Labels: c: crash,platform-android,engine,a: existing-apps,P2,a: plugins,team-android,triaged-android

I am currently working on a project in which a Flutter module is packaged into an AAR and imported into an Android project, which in turn will then be shipped as an Android library, abstracting Flutter to the end user. In order to achieve this, a FlutterActivity or FlutterFragment was not used and instead a custom view was created which wraps a FlutterView backed by a FlutterEngine. This allows complete flexibility to the consumers of the library without even having to know that Flutter is a dependency.

While this setup works perfectly, I am having some issues with the url_launcher library. The Flutter module makes use of this library to open URL's on Android, and while this works well within the Flutter module, once embedded into an Android app in the method described above, the following error is thrown whenever a URL is opened:

```
E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(NO_ACTIVITY, Launching a URL requires a foreground activity., null)
    #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
    #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
    <asynchronous suspension>
    #2      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:329:12)
    #3      MethodChannelUrlLauncher.launch (package:url_launcher_platform_interface/method_channel_url_launcher.dart:39:21)
    #4      launch (package:url_launcher/url_launcher.dart:86:58)
    #5      RouteManager.start.<anonymous closure> (package:engine_native/managers/route_manager.dart:23:19)
    <asynchronous suspension>
    #6      _rootRunUnary (dart:async/zone.dart:1134:38)
    #7      _CustomZone.runUnary (dart:async/zone.dart:1031:19)
    #8      _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
    #9      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
    #10     _DelayedData.perform (dart:async/stream_impl.dart:594:14)
    #11     _StreamImplEvents.handleNext (dart:async/stream_impl.dart:710:11)
    #12     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:670:7)
    #13     _rootRun (dart:async/zone.dart:1122:38)
    #14     _CustomZone.run (dart:async/zone.dart:1023:19)
    #15     _CustomZone.runGuarded (dart:async/zone.dart:925:7)
    #16     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
    #17     _rootRun (dart:async/zone.dart:1126:13)
    #18     _CustomZone.run (dart:async/zone.dart:1023:19)
    #19     _CustomZone.runGuarded (dart:async/zone.dart:925:7)
    #20     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
    #21     _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
    #22     _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)
```

Upon further investigation, the issue seems to be that the `UrlLauncherPlugin` relies on the `ActivityAware` class to retrieve the Activity, however for me `onAttachedToActivity` is never called. According to the documentation, the `onAttachedToActivity` in the `ActivityAware` class retrieves the activity in the following two scenarios:

> - This ActivityAware FlutterPlugin was just added to a FlutterEngine that was already connected to a running Activity.
> - This ActivityAware FlutterPlugin was already added to a FlutterEngine and that FlutterEngine was just connected to an Activity.

Given that FlutterEngine lives in a custom view that is created within an Activity, shouldn't `onAttachedToActivity` be called automatically?

Any help with this issue would be appreciated. Thanks in advance.