欢迎转载,并标明来源juejin.im/post/5cc155…
今天尝试了在原生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;
}
}复制代码
最根本的原因是我的FlutterFragment没有触发onCreateView,导致Flutter.createView(getActivity(), getLifecycle(), mRoute)
没有执行。