How can I handle incoming Intents from external applications in Flutter with android:launchMode="singleInstance"

1,512

Solution 1

The answer to this question could be the uni_links package.

You simply set up a listener to the incoming App/Deep links and call whatever methods you need :)

I do this like that:

_linkStream = getUriLinksStream().listen((uri) {
  if (uri.host == 'redirect' && uri.queryParameters.containsKey('code')) { // Checking the url

    // Do my stuff here

    _linkStream.cancel();
  }
});

The _linkStream is a StreamSubscription<Uri> property in my class. Also, don't forget to dispose() the subscription in your dispose() method!

Solution 2

I managed to get the intent in android:launchMode="singleInstance" (or singleTask) with this plugin receive_sharing_intent.

There it works with this method for texts:

ReceiveSharingIntent.getTextStream().listen((String value) {
  setState(() {
    _sharedText = value;
  });
}, onError: (err) {
  print("getLinkStream error: $err");
});

Currently (September 24th) the plugin also supports pictures and videos.

However, I didn't delve any further into the plugin, so I can't say how to implement it directly with Java/Kotlin.

Share:
1,512
Ride Sun
Author by

Ride Sun

Developed in dotnet/C++ and for years and diving into flutter and dart and loving it.

Updated on December 03, 2022

Comments

  • Ride Sun
    Ride Sun 11 months

    In the Flutter documentation here under How do I handle incoming Intents from external applications in Flutter the Manifest shows android:launchMode="singleTop" and the shared text is transfered in flutter void initState() with the getSharedText(); method in a state variable.

    Anyhow, everytime I share text to the app a new instance of the app is created. So I changed the manifest to the Manifest to android:launchMode="singleInstance" (or singleTask).

    In this case void initState() is only called once and can not be used to call getSharedText(); anymore. I tried to use AppLifecycleState.resumed to call getSharedText(); there but the data is always null. I wish flutter would have example projects for this. I could not find them. Any hints?