blog personal
Error de inicio de aplicación de análisis de la actividad en el modo estándar
En Android
el inicio de las Activity
carreras en la pila tarea correspondiente. Si directamente Application
al 标准模式
empezar la actividad, se informará del error siguiente (Android7, excepto Android8, se analizará más adelante):
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)
Mensaje de error provocó FLAG_ACTIVITY_NEW_TASK
la bandera de.
La actividad en la llamada getApplication()
o getApplicationContext()
, finalmente adquiridos son de aplicación, a continuación, getApplication().startActivity()
o getApplicationContext().startActivity()
método en la Aplicación
@Override
public void startActivity(Intent intent) {
mBase.startActivity(intent);
}
La aplicación del método en años startActivity, pasó mBase
a llamar startActivity, y es MBase ContextImpl
. método ContextImpl de startActivity
@Override
public void startActivity(Intent intent) {
warnIfCallingFromSystemProcess();
startActivity(intent, null);
}
Este método se llama a otro método sobrecargado
//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, en este método, podemos ver la excepción en el anterior lanzada. En esta sentencia, si no aumenta FLAG_ACTIVITY_NEW_TASK
la Bandera, se lanzará una excepción.
Mira 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);
}
Se puede ver en Android7, relativamente Android6, también arrojado necesita para cumplir con varias otras condiciones. Desde el punto de vista Método de aplicación startActivity ha descrito anteriormente, el tipo de paquete del parámetro de opciones es nula, por lo que esta condición no se cumple, no excepción.
Mira 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);
}
Y Android7 como también para determinar las opciones, porque las opciones es nula, y por lo tanto no se cumplen las condiciones, no será una excepción.
Echemos un vistazo en el 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);
}
En Android9, para las opciones == null || con el juez, luego ActivityOptions.fromBundle (opciones) .getLaunchTaskId () == -1, condición o se creará, o va a lanzar una excepción.
Se puede ver en Android7 y Android8, puede empezar directamente en la actividad de las aplicaciones, sin la necesidad de aumentar la FLAG_ACTIVITY_NEW_TASK bandera.