背景
? 工程使用了混合開發(fā),使用flutter boost插件,flutter 的activity1 frament1 跳轉(zhuǎn)activity2 frament2,frament1 包含platformView,按照上面老哥解決崩潰問題的基礎(chǔ)上,出現(xiàn)activity2 frament2返回activity1 frament1時(shí),platformView觸摸事件沒有響應(yīng)。問題具體見:https://github.com/alibaba/flutter_boost/issues/1755
分析
? ?項(xiàng)目主頁使用platview實(shí)現(xiàn)了webview功能,即ctivity1 frament1為主頁。具體見下圖所示
?使用AndroidView,flutter3.7上底層使用PlatformViewWrapper作為容器,底層事件是先被PlatformViewWrapper接收,在onTouchEvent中分發(fā)到Dart,然后到dart層,然后再分發(fā)到PlatformViewsController分發(fā)到platformView,具體見:Flutter 之快速理解混合開發(fā)里的手勢事件傳遞 - 掘金
? 調(diào)試時(shí),發(fā)現(xiàn)activity2 frament2返回時(shí),在activity1 frament1 已經(jīng)在PlatformViewsController已經(jīng)attach,activity2的onDestroy觸發(fā)flutterboostfragment的onDetach,最終調(diào)用了PlatformViewsController又執(zhí)行一次detach方法,導(dǎo)致platformViewsChannel被置為空,觸摸事件沒法傳遞過來
/**
- Detaches this platform views controller.
-
This is typically called when a Flutter applications moves to run in the background, or is
- destroyed. After calling this the platform views controller will no longer listen to it's
- previous messenger, and will not maintain references to the texture registry, context, and
- messenger passed to the previous attach call.
*/
@UiThread
public void detach() {
if (platformViewsChannel != null) {
platformViewsChannel.setPlatformViewsHandler(null);
}
destroyOverlaySurfaces();
platformViewsChannel = null;
context = null;
textureRegistry = null;
}
解決
? 把platformViewsChannel重新attach,恢復(fù)platformViewsChannel,能讓activity1 frament1 的platformView重新獲取觸摸事件。文章來源:http://www.zghlxwxcb.cn/news/detail-538796.html
實(shí)現(xiàn):
activity2的onDestroy時(shí),異步去調(diào)用 flutterEngine.getPlatformViewsController().attach(getContextActivity(), flutterEngine.getRenderer(), flutterEngine.getDartExecutor()); 前提是判斷frament1處于可見狀態(tài),且處于detach狀態(tài)
兼容:
PlatformViewsController在attach時(shí)有個(gè)判斷context是否為空,如下所示:
public void attach(
@nullable?Context context,
@nonnull?TextureRegistry textureRegistry,
@nonnull?DartExecutor dartExecutor) {
if (this.context != null) {
throw new AssertionError(
"A PlatformViewsController can only be attached to a single output target.\n"
+ "attach was called while the PlatformViewsController was already attached.");
}
this.context = context;
this.textureRegistry = textureRegistry;
platformViewsChannel = new PlatformViewsChannel(dartExecutor);
platformViewsChannel.setPlatformViewsHandler(channelHandler);
}
因此在上面老哥重寫的FlutterBoostPlatformViewsController中覆蓋attach方法添加detach()下兼容處理,避免快速切換,context不為空的AssertionError問題
@OverRide
public void attach(
@nullable?Context context,
@nonnull?TextureRegistry textureRegistry,
@nonnull?DartExecutor dartExecutor) {
//fixMe super.attach里面需要context為空,否則直接拋異常,而在生命周期里面設(shè)置會(huì)因?yàn)榭焖冱c(diǎn)擊不夠?qū)崟r(shí)設(shè)置導(dǎo)致異常
//因此,在attach之前先重置下,當(dāng)官方解決問題可以移除這個(gè)代碼
detach();
super.attach(context,textureRegistry,dartExecutor);
}文章來源地址http://www.zghlxwxcb.cn/news/detail-538796.html
到了這里,關(guān)于flutter3.7版本下使用flutter boost解決使用platview崩潰或異常問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!