|
20 | 20 | import io.flutter.plugin.common.MethodChannel; |
21 | 21 | import io.flutter.plugin.common.MethodChannel.MethodCallHandler; |
22 | 22 | import io.flutter.plugin.common.PluginRegistry; |
| 23 | +import io.flutter.embedding.engine.plugins.FlutterPlugin; |
| 24 | +import io.flutter.embedding.engine.plugins.activity.ActivityAware; |
| 25 | +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; |
| 26 | +import io.flutter.plugin.common.BinaryMessenger; |
23 | 27 |
|
24 | 28 | /** |
25 | 29 | * FlutterWebviewPlugin |
26 | 30 | */ |
27 | | -public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.ActivityResultListener { |
| 31 | +public class FlutterWebviewPlugin implements MethodCallHandler, FlutterPlugin, ActivityAware, PluginRegistry.ActivityResultListener { |
| 32 | + // private Activity activity; |
| 33 | + // private WebviewManager webViewManager; |
| 34 | + // private Context context; |
| 35 | + // static MethodChannel channel; |
| 36 | + // private static final String CHANNEL_NAME = "flutter_webview_plugin"; |
| 37 | + // private static final String JS_CHANNEL_NAMES_FIELD = "javascriptChannelNames"; |
| 38 | + |
| 39 | + // public static void registerWith(PluginRegistry.Registrar registrar) { |
| 40 | + // if (registrar.activity() != null) { |
| 41 | + // channel = new MethodChannel(registrar.messenger(), CHANNEL_NAME); |
| 42 | + // final FlutterWebviewPlugin instance = new FlutterWebviewPlugin(registrar.activity(), registrar.activeContext()); |
| 43 | + // registrar.addActivityResultListener(instance); |
| 44 | + // channel.setMethodCallHandler(instance); |
| 45 | + // } |
| 46 | + // } |
| 47 | + |
| 48 | + // FlutterWebviewPlugin(Activity activity, Context context) { |
| 49 | + // this.activity = activity; |
| 50 | + // this.context = context; |
| 51 | + // } |
| 52 | + |
28 | 53 | private Activity activity; |
29 | 54 | private WebviewManager webViewManager; |
30 | 55 | private Context context; |
31 | 56 | static MethodChannel channel; |
32 | 57 | private static final String CHANNEL_NAME = "flutter_webview_plugin"; |
33 | 58 | private static final String JS_CHANNEL_NAMES_FIELD = "javascriptChannelNames"; |
34 | 59 |
|
35 | | - public static void registerWith(PluginRegistry.Registrar registrar) { |
36 | | - if (registrar.activity() != null) { |
37 | | - channel = new MethodChannel(registrar.messenger(), CHANNEL_NAME); |
38 | | - final FlutterWebviewPlugin instance = new FlutterWebviewPlugin(registrar.activity(), registrar.activeContext()); |
39 | | - registrar.addActivityResultListener(instance); |
40 | | - channel.setMethodCallHandler(instance); |
| 60 | + @Override |
| 61 | + public void onAttachedToEngine(FlutterPluginBinding binding) { |
| 62 | + BinaryMessenger messenger = binding.getBinaryMessenger(); |
| 63 | + channel = new MethodChannel(messenger, CHANNEL_NAME); |
| 64 | + channel.setMethodCallHandler(this); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void onDetachedFromEngine(FlutterPluginBinding binding) { |
| 69 | + if (channel != null) { |
| 70 | + channel.setMethodCallHandler(null); |
| 71 | + channel = null; |
41 | 72 | } |
42 | 73 | } |
43 | 74 |
|
44 | | - FlutterWebviewPlugin(Activity activity, Context context) { |
45 | | - this.activity = activity; |
46 | | - this.context = context; |
| 75 | + @Override |
| 76 | + public void onAttachedToActivity(ActivityPluginBinding binding) { |
| 77 | + activity = binding.getActivity(); |
| 78 | + context = activity.getApplicationContext(); |
| 79 | + binding.addActivityResultListener(this); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void onDetachedFromActivityForConfigChanges() { |
| 84 | + if (activity != null) { |
| 85 | + activity = null; |
| 86 | + context = null; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) { |
| 92 | + activity = binding.getActivity(); |
| 93 | + context = activity.getApplicationContext(); |
| 94 | + binding.addActivityResultListener(this); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void onDetachedFromActivity() { |
| 99 | + if (activity != null) { |
| 100 | + activity = null; |
| 101 | + context = null; |
| 102 | + } |
47 | 103 | } |
48 | 104 |
|
49 | 105 | @Override |
|
0 commit comments