简体   繁体   English

如何在 FlutterActivity 上实现 onActivityResult 和 onNewIntent

[英]How can I implement onActivityResult and onNewIntent on FlutterActivity

I would like to implement onActivityResult and onNewIntent on my Android native side of flutter app.我想在我的 Flutter 应用程序的 Android 本机端实现 onActivityResult 和 onNewIntent 。

For example, I would like to be able to run "startActivityForResult", and open my app using a deep link.例如,我希望能够运行“startActivityForResult”,并使用深层链接打开我的应用程序。

My main activity is flutter, but I have some work I need to do on the native part.我的主要活动是颤振,但我有一些工作需要在本机部分做。

Is there a way to accomplish this without writing an external plugin?有没有办法在不编写外部插件的情况下实现这一点?

package com.test.flutter_app

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}

As I understand from the documentation , I need to use PluginRegistry.Registrar addActivityResultListener .正如我从文档中了解到的,我需要使用PluginRegistry.Registrar addActivityResultListener But who is my Registrar?但谁是我的注册员? how can I get it/use it?我怎样才能得到/使用它? Code examples would be much appreciated.代码示例将不胜感激。

FlutterActivity is an activity so I guess you could add something like this: FlutterActivity 是一个活动,所以我想你可以添加这样的东西:

Result mResult;

private void doStuff() {
    ...
    startActivityForResult(intent, INTENT_CODE);
}

@Override
public boolean onActivityResult(int requestCode, int resultCode, Intent intent) {
    ...
    mResult.success(null);
    return false;
}

Now you can use doStuff using a platformchannel现在您可以使用平台doStuff使用doStuff

    new MethodChannel(getFlutterView(), MY_CHANNEL).setMethodCallHandler(
            (call, result) -> {
                if (call.method.equals("my_method")) {
                    mResult = result;
                    doStuff();
                }
            });

The FlutterActivity class implements the PluginRegistry interface. FlutterActivity类实现PluginRegistry接口。 The PluginRegistry interface defines a method registrarFor which is what you want. PluginRegistry接口定义了一个registrarFor方法,这正是您想要的。

https://api.flutter.dev/javadoc/io/flutter/plugin/common/PluginRegistry.html#registrarFor-java.lang.String- https://api.flutter.dev/javadoc/io/flutter/plugin/common/PluginRegistry.html#registrarFor-java.lang.String-

So in your MainActivity class, you can do this.registrarFor('plugin_name_here') to get the registrar.因此,在您的MainActivity类中,您可以执行this.registrarFor('plugin_name_here')来获取注册商。

I think that you'll then want to set up an EventChannel .我认为你会想要设置一个EventChannel In your onActivityResult and onNewIntent methods, you can use the EventChannel to send data from the Kotlin code to the Dart code.在您的onActivityResultonNewIntent方法中,您可以使用EventChannel将数据从 Kotlin 代码发送到 Dart 代码。

No need to write a plugin, You can use MethodChannel for platform-specific code.无需编写插件,您可以使用 MethodChannel 进行平台特定的代码。

https://flutter.dev/docs/development/platform-integration/platform-channels https://flutter.dev/docs/development/platform-integration/platform-channels

I use this way.我用这种方式。

Create Channel创建频道

MethodChannel nativeChannel = new MethodChannel(MainApplication.flutterActivity.getFlutterView(), CHANNEL_NATIVE);
nativeChannel.setMethodCallHandler(new MethodCallHandler() {
    @Override
    public void onMethodCall(@NotNull MethodCall methodCall, @NotNull Result result) {
        if (methodCall.method == METHOD_SHOW_NATIVEVIEW) {
            Intent intent = new Intent(MainApplication.instance().getApplicationContext(), [YourActivityClass]);
            intent.putExtra(ARG_VIEW_NAME, [YourActivityClassName]);
            MainFlutterActivity.instance().startActivityForResult(intent, RESULT_ACTIVITY);
        }
    }
});
public void closeNativeView(String viewName, HashMap<String, Object> params) {
    HashMap<String, Object> arg = new HashMap<>();
    arg.put(ARG_VIEW_NAME, viewName);
    arg.put(ARG_PARAMS, params);
    nativeChannel.invokeMethod(METHOD_CLOSE_NATIVEVIEW, arg, null);
}

Override FlutterActivity覆盖 FlutterActivity

public class MainFlutterActivity extends FlutterActivity {
  @Override
  protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_ACTIVITY) {
      String viewName = "";
      HashMap<String, Object> params = new HashMap<String, Object>();

      if (data != null) {
        viewName = data.getStringExtra(ARG_VIEW_NAME);
        params = (HashMap<String, Object>) data.getSerializableExtra(ARG_PARAMS);
      }

      closeNativeView(viewName, params);
    }
  }
}

After some research and debate on this issue, ended up reviewing a class used on a plugin for just this purpose.在对这个问题进行了一些研究和辩论之后,最终为了这个目的审查了一个插件上使用的类。 Linker for Flutter implements a PluginRegistry.ActivityResultListener interface on the plugin class, effectively allowing you to do what you are looking for. Flutter 链接器在插件类上实现了一个 PluginRegistry.ActivityResultListener 接口,有效地允许你做你想要的。 Check it out here:在这里查看:

Linker链接器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在适配器内实现onActivityResult? - How can I implement onActivityResult inside an Adapter? 如何实现扩展WebView类的“ onActivityResult” - How can I implement “onActivityResult” extending WebView Class 我怎样才能让 OnNewIntent 开火 - How can I get OnNewIntent to fire appwidgetprovider可以实现onactivityresult怎么使用? - appwidgetprovider can implement onactivityresult how can use it? 我应该如何实现onActivityResult方法 - How should I implement onActivityResult method 如何在活动中传递onActivityResult数据? - How can i pass onActivityResult data in activity? 如何获取文件名 onActivityResult? - How can I get a file name onActivityResult? 我们如何在任何活动中使用 onNewIntent() ? - How we can use onNewIntent() in any Activity? 如果需要在onNewIntent()中或在运行时更改后添加一个Fragment,如何避免IllegalStateException? - How can I avoid an IllegalStateException if I need to add a Fragment in onNewIntent() or after a run-time change? 如何使用 Navigation Compose 将数据从 Activity 的 onNewIntent() 发送到屏幕的 VM? - How can I send data from a onNewIntent() of the Activity to the VM of a Screen using Navigation Compose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM