当前位置:网站首页>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 )
边栏推荐
- 空间三点画圆代码
- Use of jumpserver
- CF. Bits And Pieces(子集状压dp + 剪枝)
- Learn redisson from scratch ------- topics (subscription and distribution)
- MySQL configuration file
- 根证书的有效期与服务器SSL证书一样长吗?
- 数组扁平化.flat(Infinity)
- ORM introduction and database operation
- Introduction to nipple music theory and personal perception
- XSS绕过姿势总结
猜你喜欢

Cf. bits and pieces (subset pressing DP + pruning)

Ionic4 learning notes 3

Use of jumpserver

Typora is still the most beautiful and beautiful document editing artifact of yyds in my heart. I believe you will never abandon it

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

Getting started with MySQL database

6126. 设计食物评分系统

【“码”力全开,“章”显实力】2022年第1季Task挑战赛贡献者榜单

How to solve the problem that yaml in idea is unrecognized or red?

Bib | mol2context vec: context aware deep network model learning molecular representation for drug discovery
随机推荐
缺失值处理
Wu Enda writes: how to establish projects to adapt to AI career
Emerging potential of interactive virtual reality technology in drug discovery
无关的表进行关联查询及null=null条件
EasyUI adds row level buttons to the DataGrid
Example of single table query in ORM student management system
Web penetration experience summary ing
Windowing function (1) - top three employees of department salary
Go language interface and type
CF. Bits And Pieces(子集状压dp + 剪枝)
Space three point circle code
【“码”力全开,“章”显实力】2022年第1季Task挑战赛贡献者榜单
16. What is the difference between target and currenttarget?
How to prepare for hyperinflation
CF lomsat gelral (heuristic merge)
Common methods of array (2)
数组对象方法 常用遍历方法&高阶函数
Baidu touch.js
jmeter -- prometheus+grafana服务器性能可视化
The drop-down list component uses iscrol JS to achieve the rolling effect of the pit encountered