Flutter 79: illustration of Android Native integrated fluterboost small attempt

I just tried to access fluterboost a few days ago. I had a preliminary understanding of its page routing jump and parameter passing. Next, I learned the basic operation of daily operation;

As a bridge between Native and FlutterBoost, data transmission between the two ends is essential; FlutterBoost also uses the same Platform Channel to bridge, the most commonly used method channel;

Plug-in registration

When Xiaocai uses the Flutter Module, it will register different plug-ins according to different business modules. Before Xiaocai tried to use Flutter only, it needs FlutterMain.startInitialization initialization, and FlutterBoost is no exception. However, according to the version iteration of FlutterBoost, its registration method has changed with it, and it is essential;

This is v1.12.13-hotfixes. You can add the pluterembedding version to Android manifest.xml to set the registration plug-in mode, and you can also register manually through the generated pluginregister mode. At this time, you do not need to inherit the fluterapplication;

// Android manifest method registration
<meta-data android:name="flutterEmbedding"
    android:value="2">
</meta-data>

// Generated plugin registration
FlutterBoost.BoostPluginsRegister pluginsRegister = new FlutterBoost.BoostPluginsRegister() {
    @Override
    public void registerPlugins(PluginRegistry mRegistry) {
        GeneratedPluginRegistrant.registerWith(FlutterBoost.instance().engineProvider());
    }
};

Create communication channel Platform Channel

When Fletter is used only, Fletter tries to create a communication channel through MethodChannel / BasicMessageChannel / EventChannel. During initialization, fletterview is required. After accessing fluterboost, Fletter creates a communication channel in MainActivity, monitors onMethodChannel, and monitors and hears methods at the Fletter end to ensure normal communication;

new MethodChannel(getFlutterView(), "test.ace.com/main").setMethodCallHandler(new MethodChannel.MethodCallHandler() {
    @Override
    public void onMethodCall(MethodCall call, MethodChannel.Result result) {
        if (call.method.equals("ace_demo_user")) {
            Toast.makeText(this, "current method by ace_demo_user", Toast.LENGTH_LONG).show();
        } else {
            result.notImplemented();
        }
    }
});

However, the main activity is treated as a fluterview, which does not meet the requirements for later maintenance and business processing. According to fluterboost issues, the channel creation method is optimized after v1.9. When initializing fluterboost, the life cycle of fluterboost is monitored. You can use the Create it in the onPluginsRegistered() method, so as to avoid the FlutterView settings for different pages;

FlutterBoost.BoostLifecycleListener boostLifecycleListener = new FlutterBoost.BoostLifecycleListener() {

    @Override
    public void onEngineCreated() {

    }

    @Override
    public void onPluginsRegistered() {
        BinaryMessenger messenger = FlutterBoost.instance().engineProvider().getDartExecutor();
        initMainMethod(messenger);
    }

    @Override
    public void onEngineDestroy() {

    }
};

private void initMainMethod(BinaryMessenger messenger) {
    MethodChannel mMainChannel = new MethodChannel(messenger, "test.ace.com/main");
    mMainChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
        @Override
        public void onMethodCall(MethodCall call, MethodChannel.Result result) {
            if (call.arguments != null) {
                Log.e("TAG", "Callback content:" + call.arguments.toString());
            }
            if (call.method != null) {
                Log.e("TAG", "Callback method:" + call.method.toString());
            }
            if (call.method.equals("ace_demo_user")) {
                result.success("");
            } else {
                result.notImplemented();
            }
        }
    });
}

Listen to closed route

When using fluterboost, it encapsulates the method of opening pageurl to open the route in the way of official website. For special page, the route animation can also be set separately, but for closing the route animation, it can't be set directly;

In the Platform, closeContainer() method is provided, and finishContainer() can close the route by obtaining the current Activity;

// Source code
public void closeContainer(IContainerRecord record, Map<String, Object> result, Map<String, Object> exts) {
    if (record == null) return;
    record.getContainer().finishContainer(result);
}

@Override
public void finishContainer(Map<String, Object> result) {
    if (result != null) {
        setBoostResult(this.host.getActivity(), new HashMap<>(result));
        this.host.getActivity().finish();
    } else {
        this.host.getActivity().finish();
    }
}

closeContainer() this method is not an abstract method, but the Platform is an abstract class, so you can rewrite closeContainer() directly;

Platform platform = new Platform() {
    @Override
    public Application getApplication() {
        return BaseApplication.getInstance();
    }

    @Override
    public void openContainer(Context context, String url, Map<String, Object> urlParams, int requestCode,
        Map<String, Object> exts) {
        router.openContainer(context, url, urlParams, requestCode, exts);
    }

    @Override
    public void closeContainer(IContainerRecord record, Map<String, Object> result, Map<String, Object> exts) {
        super.closeContainer(record, result, exts);
        // Get route Activity to set transition animation
    }

    @Override
    public int whenEngineStart() {
        return FlutterBoost.ConfigBuilder.ANY_ACTIVITY_CREATED;
    }

    @Override
    public FlutterView.RenderMode renderMode() {
        return FlutterView.RenderMode.texture;
    }

    @Override
    public boolean isDebug() {
        return true;
    }

    @Override
    public String initialRoute() {
        return "/";
    }
};

Small expansion

Cause: assert appproject! = null: when integrating Flutter and fluterboost in a historical project;

Cause: assert appProject != null
|          |
null       false

The main Module of the history project of the small dish is to replace news with app. This problem is not a big problem. I hope that friends with the same problem can avoid it in advance;

Xiaocai's research on Flutter and fluterboost is not deep enough, and she is still learning and exploring. If there is any mistake, please give more guidance!

Source: little monk aze

Keywords: Android xml

Added by pyro3k on Mon, 09 Mar 2020 07:45:27 +0200