alibaba / flutter_boost

FlutterBoost is a Flutter plugin which enables hybrid integration of Flutter for your existing native apps with minimum efforts
https://github.com/alibaba/flutter_boost
MIT License
6.98k stars 1.23k forks source link

[Bug]: 同一个FragmentActivity,2个包含webview flutter 页面的Fragment,第二个打开,客户端崩溃 #1948

Open yangwping opened 10 months ago

yangwping commented 10 months ago

请描述遇到的问题,以及您所期望的正确的结果

同一个FragmentActivity,2个包含webview的Fragment,使用example中 open flutter fragment page 把里面第二个和三个fragment都改成包含webview的Flutter页面(url为 platformview/simplewebview ),打开第二个fragment之后,再打开第三个fragment,客户端崩溃,错误日志如下:

Process: com.idlefish.flutterboost.example, PID: 31135
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:5109)
    at android.view.ViewGroup.addView(ViewGroup.java:4940)
    at android.view.ViewGroup.addView(ViewGroup.java:4880)
    at android.view.ViewGroup.addView(ViewGroup.java:4853)
    at io.flutter.plugin.platform.PlatformViewsController.attachToView(PlatformViewsController.java:798)
    at io.flutter.embedding.android.FlutterView.attachToFlutterEngine(FlutterView.java:1215)
    at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onCreateView(FlutterActivityAndFragmentDelegate.java:338)
    at io.flutter.embedding.android.FlutterFragment.onCreateView(FlutterFragment.java:806)
    at com.idlefish.flutterboost.containers.FlutterBoostFragment.onCreateView(FlutterBoostFragment.java:102)
    at com.idlefish.flutterboost.example.tab.FriendFlutterFragment2.onCreateView(FriendFlutterFragment2.java:19)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
    at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:434)
    at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
    at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
    at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
    at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
    at android.os.Handler.handleCallback(Handler.java:907)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7779)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:990)

请说明如何操作会遇到上述问题

同一个FragmentActivity,2个包含webview的Fragment,使用example中 open flutter fragment page 把里面第二个和三个fragment都改成包含webview的Flutter页面(url为 platformview/simplewebview ),打开第二个fragment之后,再打开第三个fragment,出现以上问题; 目前分析来看,是由于单引擎导致,这个有计划兼容吗?

在下面填入关键复现代码

使用的example 例子的 com.idlefish.flutterboost.example.tab.TabMainActivity 页面,修改代码如下:

    private FriendFlutterFragment friendFragment;
    public FriendFlutterFragment2 nativeFragment;
        friendFragment = new FriendFlutterFragment
                .CachedEngineFragmentBuilder(FriendFlutterFragment.class)
                .url("platformview/simplewebview")
                .build();

        nativeFragment = new FriendFlutterFragment2
                .CachedEngineFragmentBuilder(FriendFlutterFragment2.class)
                .url("platformview/simplewebview")
                .build();

复现的平台

Android

Flutter SDK版本

3.7.7

FlutterBoost版本

4.4.2

是否延迟初始化FlutterBoost

No

解决方案

0xZOne commented 10 months ago

从描述看,这个问题的主要原因不在于是否FlutterBoost支持多引擎,而是官方的PlatformView对单引擎复用支持不太友好导致~

yangwping commented 10 months ago

从描述看,这个问题的主要原因不在于是否FlutterBoost支持多引擎,而是官方的PlatformView对单引擎复用支持不太友好导致~

我现在在 FlutterBoostFragment 的 didFragmentHide 的时候调用detachFromEngineIfNeeded ,但是修改之后,每次切换fragment的时候,会闪一下屏,然后正常显示。

除此之外,还有什么好的方法吗?

0xZOne commented 10 months ago

从描述看,这个问题的主要原因不在于是否FlutterBoost支持多引擎,而是官方的PlatformView对单引擎复用支持不太友好导致~

我现在在 FlutterBoostFragment 的 didFragmentHide 的时候调用detachFromEngineIfNeeded ,但是修改之后,每次切换fragment的时候,会闪一下屏,然后正常显示。

除此之外,还有什么好的方法吗?

这个得具体问题具体分析,要看闪屏的原因是什么?

Andrew-Shi commented 9 months ago
image

添加这几行代码试试呢

koukoucai commented 6 months ago

image 添加这几行代码试试呢

image 添加这几行代码试试呢

亲测好使

wanhui123 commented 3 months ago

@Andrew-Shi 你这必需把源码下载下来改吧

wanhui123 commented 2 months ago

企业微信截图_84142f4c-6904-40a5-ae6f-1988241f754f