当前位置:网站首页>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:25:00 【Sun dried old salted fish】
Recently, I was working on a project related to human face recognition , call context.startActivity(intent) Method , An error is as follows :
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
analysis :Activity Inherited from Context, see Context.startActivity(Intent, Bundle), The following figure is part of the method annotation :

explain :
If this method is not Activity Of Context call , So this Intent Must include Intent.FLAG_ACTIVITY_NEW_TASK This flag, If it is Activity Of Context call , You don't need to .
This is because , If not by an existing Activity start-up , There is no existing stack to replace the new Activity, So it needs to be in its own stack . So you need this Intent Set startup parameters Intent.FLAG_ACTIVITY_NEW_TASK This flag, Make the new Activity In its own stack .
Because I am in React-native Through the custom module , Give Way react-native call android The native interface (Android native and React-native Call each other and pass parameters _ Dried old salted fish blog -CSDN Blog ), This custom module The initialization is as follows :

Definition Context When you use
private ReactApplicationContext mContext;In use , First judge ReactApplicationContext Is there any currentActivity, If there is , Get it Activity object , Then start the new through this object 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, " Brush your face successfully !");
} else {
Log.d(TAG, " Face brushing failed !");
}
}
// After brushing your face , Release resources in time
WbCloudFaceVerifySdk.getInstance().release();
}
});
}
// callback.invoke(STATUS_SUCCESS);
}Why not define it at the beginning of initialization mContext by Activity Of Context, Please refer to the following instructions (ReactContextBaseJavaModule getCurrentActivity Return empty question _ Dried old salted fish blog -CSDN Blog )
边栏推荐
- Namespace: cluster environment sharing and isolation
- Encapsulate function basedata.js
- Install jumpserver
- The collapse of margin
- 6126. 设计食物评分系统
- Laravel notes - RSA encryption of user login password (improve system security)
- Emerging potential of interactive virtual reality technology in drug discovery
- Mid year inventory | in 2022, PAAS will be upgraded again
- Typora 它依然是我心中的YYDS 最优美也是颜值最高的文档编辑神器 相信你永远不会抛弃它
- 线段树合并板子
猜你喜欢

Go language file operation

Cf. bits and pieces (subset pressing DP + pruning)
![[OBS] dependency Library: x264 vs Build](/img/24/4caea5dc6ff092a3161f43b6026d25.png)
[OBS] dependency Library: x264 vs Build

The 5th Digital China Construction summit opened in Fuzhou, Fujian

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag

9. BOM object?

【刷题记录】20. 有效的括号

About the writing method of interface 1 chain interpretation 2. Method execution (finally) must be executed

Inheritance and Derive

6126. 设计食物评分系统
随机推荐
Template syntax [easy to understand]
Inoic4 learning notes 2
Latex数学公式
怎么解决idea中yaml无法识别或者飘红?
BOM understanding in odoo
[OBS] dependency Library: x264 vs Build
The 5th Digital China Construction summit opened in Fuzhou, Fujian
IO multiplexing
Namespace: cluster environment sharing and isolation
Is the validity period of the root certificate as long as the server SSL certificate?
odoo中的bom理解
6. How to add an array in Es5?
Emerging potential of interactive virtual reality technology in drug discovery
MySQL configuration file
排序的几种方式for while 还有sort
undefined reference to H5PTopen
How to quickly upload files to Google Lab
Typora 它依然是我心中的YYDS 最优美也是颜值最高的文档编辑神器 相信你永远不会抛弃它
CF. Bits And Pieces(子集状压dp + 剪枝)
Baidu touch.js