当前位置:网站首页>Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
2022-07-24 18:23:00 【晒干的老咸鱼】
最近在做一个人脸识别相关的项目,调用context.startActivity(intent)方法,报错如下:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
分析:Activity继承自Context,查看Context.startActivity(Intent, Bundle),下图是该方法注释的一部分:

说明:
如果这个方法被一个不是Activity的Context调用,那么这个Intent必须包括Intent.FLAG_ACTIVITY_NEW_TASK这个flag,如果是被Activity的Context调用,则不需要。
这是因为,如果不是被一个已存在的Activity启动,就没有已存在的栈去替换新的Activity,所以需要让它在自身的栈中。所以需要这个Intent设置启动参数Intent.FLAG_ACTIVITY_NEW_TASK这个flag,让新Activity在它自身的栈中。
由于我是在React-native当中通过自定义模块,让react-native调用android原生接口(Android native和React-native相互调用和传参_晒干的老咸鱼的博客-CSDN博客),该自定义的module初始化如下:

定义Context的时候使用
private ReactApplicationContext mContext;在使用的时候,先判断ReactApplicationContext有没有currentActivity,如果有,就拿到Activity对象,然后通过该对象来启动新的Activity。
if (mContext.hasCurrentActivity()){
Activity currentActivity = mContext.getCurrentActivity();
Log.d(TAG, "currentActivity: " + currentActivity);
WbCloudFaceVerifySdk.getInstance().startWbFaceVerifySdk(currentActivity, new WbCloudFaceVerifyResultListener() {
@Override
public void onFinish(WbFaceVerifyResult result) {
if (result != null) {
if (result.isSuccess()) {
Log.d(TAG, "刷脸成功!");
} else {
Log.d(TAG, "刷脸失败!");
}
}
//刷脸结束后,及时释放资源
WbCloudFaceVerifySdk.getInstance().release();
}
});
}
// callback.invoke(STATUS_SUCCESS);
}这里为什么不在一开始初始化的时候定义mContext为Activity的Context,可以参考如下说明(ReactContextBaseJavaModule getCurrentActivity 返回空问题_晒干的老咸鱼的博客-CSDN博客)
边栏推荐
- CF lomsat gelral (heuristic merge)
- 数组常用方法(2)
- Read zepto source code touch module
- 怎么解决idea中yaml无法识别或者飘红?
- undefined reference to H5PTopen
- Latex数学公式
- 数组扁平化.flat(Infinity)
- 继承与派生
- ["code" power is fully open, and "chapter" shows strength] list of contributors to the task challenge in the first quarter of 2022
- Techempower web framework performance test 21st round results release --asp Net core continue to move forward
猜你喜欢
随机推荐
Read zepto source code touch module
Web penetration experience summary ing
排序的几种方式for while 还有sort
Three ways of redis cluster
Array object methods commonly used traversal methods & higher-order functions
mysql 配置文件
Model saving and loading of sklearn
Alibaba /1688 API instructions for searching products by map (pailitao)
sklearn 的模型保存与加载使用
["code" power is fully open, and "chapter" shows strength] list of contributors to the task challenge in the first quarter of 2022
Space three point circle code
redis集群的三种方式
Pycharm configuring opencv Library
Section 11 cache avalanche, hot data failure follow Daewoo redis ------- directory post
Alibaba 1688 keyword search product API usage display
Framework introduction
Variable and immutable data types
Wu Enda writes: how to establish projects to adapt to AI career
Four ways of simple interest mode
Highcharts chart and report display, export data









