flutter / website

Flutter documentation web site
https://docs.flutter.dev
Other
2.82k stars 3.23k forks source link

Update Android Intent handling on 'Flutter for Android developers' page #4497

Open palucdev opened 4 years ago

palucdev commented 4 years ago

Hello!

I have simple communication case where android application is launching flutter application with intent containing some extra string data (using putExtra() method). Found official docs with guide how to handle such situations but unfortunetaly provided code from docs do not work as it uses io.flutter.app.FlutterActivity package.

Using io.flutter.embedding.android.FlutterActivity results in problem with GeneratedPluginRegistrant.registerWith(this); line, as .registerWith() expects to have FlutterEngine param, not MainActivity one.

Using suggested code for io.flutter.embedding.android.FlutterActivity :

@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
    GeneratedPluginRegistrant.registerWith(flutterEngine);
}

results in problem with MethodChannel seen below (do I still need to call some registration method in onCreate() )?

MissingPluginException(No implementation found for method getSharedText on channel app.channel.shared.data)
#0     MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:319:7)

Can someone provide me with usable solution on how to handle Intents in Flutter (v1.12.13+hotfix.9)? It will be good to update docs too, now it is more confusing than helpful.

Thanks in advance!

palucdev commented 4 years ago

For everyone interested, I managed to get it to work overriding configureFlutterEngine() method of io.flutter.embedding.android.FlutterActivity in the MainAcrivity class:

  @Override
  public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
    GeneratedPluginRegistrant.registerWith(flutterEngine);
    Intent intent = getIntent();

    handleSendText(intent);

    new MethodChannel(getFlutterView(), "app.channel.shared.data").setMethodCallHandler(
            (call, result) -> {
              if (call.method.contentEquals("getSharedText")) {
                result.success(sharedText);
                sharedText= null;
              }
            }
    );
  }

  void handleSendText(Intent intent) {
    sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
  }

  BinaryMessenger getFlutterView() {
    return getFlutterEngine().getDartExecutor().getBinaryMessenger();
  }

Still it will be good to update official docs :)

sfshaza2 commented 4 years ago

This issue is filed against flutter.dev, so should be moved to that repo.

@xster, do we have a solution for Kotlin?

miquelbeltran commented 1 year ago

I just checked the code in https://docs.flutter.dev/get-started/flutter-for/android-devs#how-do-i-handle-incoming-intents-from-external-applications-in-flutter, and it works as expected! looks like someone already fixed it but forgot to close this issue.

The only thing that could be improved is migrating the sample code to Kotlin, but I am not sure if is there a general goal to have all Java samples migrated to Kotlin on the website.