blog pessoal
Iniciar aplicação de erro Análise Atividade no modo padrão
No Android
início das Activity
corridas na pilha tarefa correspondente. Se você diretamente Application
para 标准模式
iniciar atividades, ele irá informar o seguinte erro (Android7, exceto Android8, serão analisados mais adiante):
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:912)
at android.app.ContextImpl.startActivity(ContextImpl.java:888)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:379)
Mensagem de erro solicitado FLAG_ACTIVITY_NEW_TASK
Bandeira de.
Atividade na chamada getApplication()
ou getApplicationContext()
, eventualmente adquirida são de aplicação, em seguida, getApplication().startActivity()
ou getApplicationContext().startActivity()
método no aplicativo
@Override
public void startActivity(Intent intent) {
mBase.startActivity(intent);
}
A aplicação do método em startActivity anos, passou mBase
a chamar startActivity e MBASE é ContextImpl
. ContextImpl método de startActivity
@Override
public void startActivity(Intent intent) {
warnIfCallingFromSystemProcess();
startActivity(intent, null);
}
Este método foi chamar outro método sobrecarregado
//Android6
@Override
public void startActivity(Intent intent, Bundle options) {
warnIfCallingFromSystemProcess();
if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
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);
}
Android6, neste método, podemos ver a exceção no anterior lançada. Neste julgamento, se não aumentar FLAG_ACTIVITY_NEW_TASK
a Bandeira, vai lançar uma exceção.
Olhe método Android7
@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.
if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0
&& 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);
}
Ele pode ser visto em Android7, relativamente Android6, também jogado precisa atender a várias outras condições. Do ponto de vista do método de startActivity Aplicação descrito acima, Bundle tipo do parâmetro opções é nulo, pelo que esta condição não for satisfeita, nenhuma exceção é lançada.
Olhe método Android8
@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.
if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0
&& 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);
}
E Android7 como também para determinar as opções, porque as opções é nulo e, portanto, não estão reunidas as condições, não vai lançar uma exceção.
Vamos olhar o método de Android9
@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);
}
Em Android9, para opções == null || com o juiz, em seguida, ActivityOptions.fromBundle (Opções) .getLaunchTaskId () == -1, condição ou será criado, ou vai accionar uma excepção.
Ele pode ser visto em Android7 e Android8, você pode iniciar diretamente na Atividade de aplicativos, sem a necessidade de aumentar a FLAG_ACTIVITY_NEW_TASK Bandeira.