在Flutter插件中获取活动引用

tzcvj98z  于 2023-06-07  发布在  Flutter
关注(0)|答案(5)|浏览(294)

当我创建flutter插件时,默认情况下plugin类中有两个方法:

override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding)

和/或

fun registerWith(registrar: Registrar)

文件上的评论说:It is encouraged to share logic between onAttachedToEngine and registerWith to keep them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called depending on the user's project. onAttachedToEngine or registerWith must both be defined in the same class.
现在,我需要从这里开始另一个活动,使用activity.startActivityForResult()。可以使用registrar.activity()获取对registerWith(registrar: Registrar)中Activity的引用。如何在onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding)方法中实现这一点?

sbdsn5lh

sbdsn5lh1#

here
实现ActivityAware,其中一个方法是

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
    this.activity = binding.activity;
  }
mum43rcc

mum43rcc2#

注:

你可以通过实现ActivityAware接口来获取对Activity的引用但是如果你在onAttachToEngine()中使用setMethodCallHandler(...)方法,onAttachToActivity()将永远不会被调用,你也永远无法访问Activity
看看下面的例子

**什么不起作用:**在下面的示例中,onAttachToActivity()从未被调用

class AndroidLongTaskPlugin : FlutterPlugin, ActivityAware {
  private var activity: FlutterActivity? = null

  

  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    //activity is null here 
    //also onAttachToActivity will never be called because we are calling setMethodHandler here
    channel = MethodChannel(binaryMessenger, CHANNEL_NAME)
    channel.setMethodCallHandler { call, result ->
        //our code
    }

  }

  override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
    channel?.setMethodCallHandler(null)
  }

  override fun onAttachedToActivity(binding: ActivityPluginBinding) {
    activity = binding.activity as FlutterActivity
  }

  //rest of the methods
}

以下是一个工作示例:

class MyPlugin : FlutterPlugin, ActivityAware {
  private var activity: FlutterActivity? = null
  private var binaryMessenger: BinaryMessenger? = null

  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    binaryMessenger = flutterPluginBinding.binaryMessenger
  }

  override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
    Log.d("DART/NATIVE", "onDetachedFromEngine")
    channel?.setMethodCallHandler(null)
  }

  override fun onAttachedToActivity(binding: ActivityPluginBinding) {
    Log.d("DART/NATIVE", "onAttachedToActivity")
    activity = binding.activity as FlutterActivity
    //here we have access to activity
    //also make sure to setMethodCallHandler here
    channel.setMethodCallHandler { call, result ->
        //our code
    }
  }

  //rest of the methods
}
o0lyfsai

o0lyfsai3#

在最新版本Flutter 3.0.5+中,这是我的工作:
确保在类实现中添加ActivityAware

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;

public class FlutterCustomPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {

    private MethodChannel channel;
    private Activity activity;

 @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_custom_plugin");
        channel.setMethodCallHandler(this);
//        channel.setMethodCallHandler(new FlutterBlufiPlugin(flutterPluginBinding.activity()));
    }

  @Override
    public void onAttachedToActivity(ActivityPluginBinding activityPluginBinding) {
        // TODO: your plugin is now attached to an Activity
        this.activity = activityPluginBinding.getActivity();
    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {
        // TODO: the Activity your plugin was attached to was destroyed to change configuration.
        // This call will be followed by onReattachedToActivityForConfigChanges().
    }

    @Override
    public void onReattachedToActivityForConfigChanges(ActivityPluginBinding activityPluginBinding) {
        // TODO: your plugin is now attached to a new Activity after a configuration change.
    }

    @Override
    public void onDetachedFromActivity() {
        // TODO: your plugin is no longer associated with an Activity. Clean up references.
    }

}

记住添加所有4个覆盖方法

r7knjye2

r7knjye24#

在2023年,你可以这样做:
Kotlin代码:

class MainActivity: FlutterActivity() {
     override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        Global_Variable.current_activity = this.activity;
        Global_Variable.application_content = this.applicationContext;

        ...
    }
}

Java代码:

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;

public class MainActivity extends FlutterActivity {

    @Override
    public void configureFlutterEngine(FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);

        // Get a reference to the Activity
        FlutterActivity mainActivity = (FlutterActivity) this;

        // Do something with the Activity
        // ...
    }
}
w1e3prcc

w1e3prcc5#

在我的情况下,alireza-easazade的分辨率不起作用。我从onAttachedToEngine删除了MethodChannel创建,但ActivityAware通知仍然没有出现。对我有用的是用flutterPluginBinding.getFlutterEngine().getDartExecutor()替换flutterPluginBinding.getBinaryMessenger()作为MethodChannel构造函数中的参数。有一次我做了这个onAttachedToActivityonAttachedToEngine之后立即调用。
下面是我的工作示例:

public class MyPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {

  private ActivityPluginBinding _activityBinding;
  private FlutterPluginBinding _flutterBinding;
  private MethodChannel _channel;

  // FlutterPlugin  overrides

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    _flutterBinding = flutterPluginBinding;
    _channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "com.example.test/myplugin");
    _channel.setMethodCallHandler(this);
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    _channel.setMethodCallHandler(null);
    _channel = null;
    _flutterBinding = null;
  }

  // ActivityAware  overrides

  @Override
  public void onAttachedToActivity​(ActivityPluginBinding binding) {
    _activityBinding = binding;
  }

  @Override
  public void   onDetachedFromActivity() {
    _activityBinding = null;
  }

  @Override
  public void   onReattachedToActivityForConfigChanges​(ActivityPluginBinding binding) {
    _activityBinding = binding;
  }

  @Override
  public void   onDetachedFromActivityForConfigChanges() {
    _activityBinding = null;
  }

  // MethodCallHandler overrides
  
  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    // Handle calls
  }

  // Implementation

  public Context getApplicationContext() {
    return (_flutterBinding != null) ? _flutterBinding.getApplicationContext() : null;
  }

  public Activity getActivity() {
    return (_activityBinding != null) ? _activityBinding.getActivity() : null;
  }
}

相关问题