Flutter提示:获取权限的Activity不存在
我的主项目是Flutter项目,里面需要跳转到原生页面,通过FlutterBoost跳转
然后从原生页面又跳转到一个新的Flutter页面,
再返回到最初的页面,表面上看不出什么问题,但是每次使用Activity的时候,比如我项目里使用了photo_manager和url_launcher,每次使用到这两个库的时候,会报Activity不存在,null。通过看源码,发现每次进入BoostFlutterActivity后,都会走
这个方法,然后会走这里
先置空再赋值
这是Activity是BoostFlutterActivity,一切也都没问题,然后当我关闭这个BoostFlutterActivity,会执行detachFromActivity()方法,会将绑定的Activity置空,这时和Activity相关的逻辑都不能用了,比如使用权限库的时候会报这个错:获取权限的Activity不存在
后来看了下flutter-boost的issue,里面有一条解决了这个问题https://github.com/alibaba/flutter_boost/issues/970。这个问题会在flutter-boost v1.22.4-hotfixes出现,模仿v1.17.1分支写就可以了,下面是我把flutter-boost代码拉到本地,然后修改的代码,在FlutterActivityAndFragmentDelegate类里面
新增了红色框中的就可以了。
'''
public void onResume() {
if(mSyncer.isLocked())return;
mSyncer.onAppear();
Log.v(TAG, "onResume()");
ensureAlive();
flutterEngine.getLifecycleChannel().appIsResumed();
if(ACTIVITY_CONTROL_SURFACE_ATTACH_TO_ACTVITY_HASH_CODE==0||
ACTIVITY_CONTROL_SURFACE_ATTACH_TO_ACTVITY_HASH_CODE!=this.host.getActivity().hashCode()){
flutterEngine.getActivityControlSurface().detachFromActivityForConfigChanges();
flutterEngine.getActivityControlSurface().attachToActivity(
host.getActivity(),
host.getLifecycle()
);
ACTIVITY_CONTROL_SURFACE_ATTACH_TO_ACTVITY_HASH_CODE=this.host.getActivity().hashCode();
}
}
'''
'''
public void onResume() {
if(mSyncer.isLocked())return;
mSyncer.onAppear();
Log.v(TAG, "onResume()");
ensureAlive();
flutterEngine.getLifecycleChannel().appIsResumed();
if(ACTIVITY_CONTROL_SURFACE_ATTACH_TO_ACTVITY_HASH_CODE==0||
ACTIVITY_CONTROL_SURFACE_ATTACH_TO_ACTVITY_HASH_CODE!=this.host.getActivity().hashCode()){
flutterEngine.getActivityControlSurface().detachFromActivityForConfigChanges();
flutterEngine.getActivityControlSurface().attachToActivity(
host.getActivity(),
host.getLifecycle()
);
ACTIVITY_CONTROL_SURFACE_ATTACH_TO_ACTVITY_HASH_CODE=this.host.getActivity().hashCode();
}
}
'''