Ride Sun
Ride Sun

Reputation: 2323

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

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?

Upvotes: 6

Views: 3130

Answers (2)

Till A. S. Friebe
Till A. S. Friebe

Reputation: 1439

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.

Upvotes: 3

Albert221
Albert221

Reputation: 7041

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!

Upvotes: 5

Related Questions