Receive Share Text intents in Flutter

In this article I'll cover how to receive share intents in Flutter. This is so that users can share an article from Chrome to your app as a link, or share a photo from their favorite photos app directly into your app.

The Flutter documentation already shows a full sample on how to do this for text at However, it's written in Java. Here's how it looks like in Kotlin:

Android side

  private var sharedText: String? = null

  override fun onCreate(savedInstanceState: Bundle?) {


    MethodChannel(flutterView, "")
            .setMethodCallHandler { call, result ->
              if (call.method.contentEquals("getSharedText")) {
                sharedText = null

  fun handleShareIntent() {
    val action = intent.action
    val type = intent.type

    if (Intent.ACTION_SEND.equals(action) && type != null) {
      if ("text/plain".equals(type)) {

  fun handleSendText(intent : Intent) {
    sharedText = intent.getStringExtra(Intent.EXTRA_TEXT)

Flutter side

On the Flutter side, it's as documented in the tutorial:

const platform = const MethodChannel('');

Future<String> getSharedText() async {
  final sharedData = await platform.invokeMethod("getSharedText");
  return sharedData;

App Manifest

Don't forget to add the share intent filter in the manifest:

    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />

Unit tests

To unit test it, use MethodChannel.setMockMethodCallHandler:

const platform = const MethodChannel('');

void main() {
  testWidgets('sends shared text', (WidgetTester tester) async {
    platform.setMockMethodCallHandler((_) {
      return Future.value('');

    final chatService = MockChatService();
    await tester.pumpWidget(MaterialApp(
        home: Scaffold(body: ChatView.withParameters(chatService))));

    await tester.pump();


Handle shared data when resuming & quick share (Nov 2019 update)


I had a few issues though. If I shared to my app, it might switch to it, but not receive the intent. The quick share UI in Chrome also didn't seem to work. The fix is to remove launchMode from my manifest:

        <!-- Remove the launchMode or set to standard! -->

And add a listener for the resume event in Flutter (but not in Android):

class _HomePageState extends State<MyHomePage> with WidgetsBindingObserver {

  void initState() {


    // Listen to lifecycle events.

  void dispose() {

  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.resumed) {

The details

Back to pure Android

In the screenshot below, if I tap "Share...", I get to pick which app I send the URL to. If I select my app, it'll correctly run it with the right share intent. However, if I tap my app's icon on the right side, it'll run my app but not update the intent.

It took a bit of trial and error because it involves both Android and Flutter. In order to test better, I decided to make a tiny standalone Android app that receives a share text intent.

One post about receiving intents in Android I found said I should add a listener on onResume. This is wrong because depending on the app manifest, onResume might run with the same intent as what started the Activity in the first place. In this case the app would process the same intent upon returning. The post that put me on the right track is this one. They say you should listen to onNewIntent instead of onResume. It turns out it also didn't work, but they referred to some launchMode in the manifest. Setting the right value for it fixed it.

android:launchMode has 4 values:

  • standard
  • singleTop
  • singleTask
  • singleInstance

Each one behaves slightly differently. My Flutter app was set to singleTop by default. What this means is that if the activity is at the top of the app stack, it'll return to that. If it's below, it'll start a new activity. That's why I sometimes ended up with several instances of my app...

I've tried the other modes and I seem to get the same issue. It always worked when first running the app. However, it would resume either by calling onNewIntent with the same intent that initially launched the activity (not the one we're sharing the second time), or simply not calling onNewIntent at all.

standard is the one that behaves as I'd expect. Every time you share, via quick share or share..., it'll run onCreate with the new intent. It'll also re-use the currently running instance and never seem to create a new instance. Isn't that the same as singleInstance? The thing is that I don't understand the concepts of instance, root task, activity in Android yet.

Trying it in Flutter

With standard mode, the Android piece starts with an onCreate and the right intent but the Flutter app itself actually just resumes. So I did need to add a listener for resume events in Flutter. To listen to resume events, you have to make your Widget use the WidgetsBindingObserver mixin. Refer to the TLDR above for the code.

Read more on mixins at