A組提供了一個(gè)定制的Actvity給客戶組的研發(fā)調(diào)用纸泡,調(diào)用定制后的Activity的startActivity后,拋出以下異常:
2020-04-08 16:36:18.820 12184-12488...
startClient exception:
android.util.AndroidRuntimeException Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:916)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:406)
at selfDefineActivity.startActivity(CounterfeitActivity.java:80)
...
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
系統(tǒng)報(bào)錯(cuò)位置
//ContextImpl.java
@Override
public void startActivity(Intent intent, Bundle options) {
warnIfCallingFromSystemProcess();
// Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is
// generally not allowed, except if the caller specifies the task id the activity should
// be launched in. A bug was existed between N and O-MR1 which allowed this to work. We
// maintain this for backwards compatibility.
final int targetSdkVersion = getApplicationInfo().targetSdkVersion;
if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
&& (targetSdkVersion < Build.VERSION_CODES.N
|| targetSdkVersion >= Build.VERSION_CODES.P)
&& (options == null
|| ActivityOptions.fromBundle(options).getLaunchTaskId() == -1)) {
throw new AndroidRuntimeException(
"Calling startActivity() from outside of an Activity "
+ " context requires the FLAG_ACTIVITY_NEW_TASK flag."
+ " Is this really what you want?");
}
mMainThread.getInstrumentation().execStartActivity(
getOuterContext(), mMainThread.getApplicationThread(), null,
(Activity) null, intent, -1, options);
}
如上所說義蔼水,如果你通過Context調(diào)用StartActivity震糖,則最總會(huì)調(diào)用ContextImpl的startActivity方法,并在方法內(nèi)部進(jìn)行檢查趴腋,只有StartActivity沒有設(shè)置FLAG_ACTIVITY_NEW_TAS吊说,且系統(tǒng)版本是7,8,并且options 為空或者啟動(dòng)的taskid為-1是优炬,才會(huì)觸發(fā)拋出異常
我們遇到的問題是因?yàn)槲覀冏远x的selfDefineActivity颁井,重新了startActivity方法,在覆寫的startActivity中通過context啟動(dòng)了另外一個(gè)Activity穿剖,且啟動(dòng)的時(shí)候Intent沒有設(shè)置對(duì)應(yīng)的FLAG_ACTIVITY_NEW_TAS蚤蔓,options,且系統(tǒng)版本為Android 8
解決方法:
Intent設(shè)置FLAG_ACTIVITY_NEW_TAS糊余,或者傳一個(gè)options對(duì)象即可