FlutterFragment 踩坑记

583 阅读1分钟

今天尝试了在原生android项目中导入flutter模块,但在使用FragmentManager切换FlutterFragment时发生了“Platform view is not attached”的异常。

(我的demo是一个原生fragment,一个FlutterFragment)


后来查看FlutterView代码时发现其中的原因,在我们创建FlutterView

Flutter.createView(getActivity(), getLifecycle(), mRoute)

绑定了外部的FlutterFragment的生命周期,当切换其他的Framgment时,此FlutterFragment自然调用了ondestroy方法,而FlutterView也通过getLifecycle监听到了也调用了自身的destroy,并且销毁了寄生的外部mNativeView。由于mNativeView为空,所以导致异常

public void destroy() {

    if (this.isAttached()) {
        this.getHolder().removeCallback(this.mSurfaceCallback);
        this.mNativeView.destroy();
        this.mNativeView = null;
    }
}