当前位置:网站首页>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 )
边栏推荐
猜你喜欢

Go language file operation

Bib | mol2context vec: context aware deep network model learning molecular representation for drug discovery

第五届数字中国建设峰会在福建福州开幕

怎么解决idea中yaml无法识别或者飘红?

["code" power is fully open, and "chapter" shows strength] list of contributors to the task challenge in the first quarter of 2022

The drop-down list component uses iscrol JS to achieve the rolling effect of the pit encountered

初识Pytorch和Pytorch环境配置

L4L7负载均衡

How to render millions of 2D objects smoothly with webgpu?

QT—动画框架
随机推荐
可撤销并查集板子
We have to understand the four scopes: application, session, request and page
Ionic4 learning notes 7 -- UI component 1 (no practice, direct excerpt)
CF Lomsat gelral(启发式合并)
【“码”力全开,“章”显实力】2022年第1季Task挑战赛贡献者榜单
jmeter --静默运行
4. Basic type and reference type?
Introduction to nipple music theory and personal perception
Example of single table query in ORM student management system
Emerging potential of interactive virtual reality technology in drug discovery
Typora is still the most beautiful and beautiful document editing artifact of yyds in my heart. I believe you will never abandon it
Is the validity period of the root certificate as long as the server SSL certificate?
Revocable search board
猜JWT关键字
文件上传漏洞——.user.ini与.htaccess
pinia 入门及使用
奶头乐理论介绍及个人感悟
空间三点画圆代码
JMeter -- silent operation
Common methods of array (2)