Blog personnel
Démarrage de l'application Analyse des erreurs d'activité en mode standard
Dans Android
le début des Activity
courses dans la pile de tâches correspondant. Si vous directement Application
à 标准模式
commencer l' activité, il signale l'erreur suivante (Android7, sauf Android8, seront analysés plus tard):
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)
Message d'erreur poussé FLAG_ACTIVITY_NEW_TASK
Drapeau.
L' activité dans l'appel getApplication()
ou getApplicationContext()
, éventuellement acquises sont d' application, alors getApplication().startActivity()
ou getApplicationContext().startActivity()
méthode dans l'application
@Override
public void startActivity(Intent intent) {
mBase.startActivity(intent);
}
Application de la méthode dans les années startActivity, passé mBase
appeler startActivity et MBASE est ContextImpl
. Procédé ContextImpl de startActivity
@Override
public void startActivity(Intent intent) {
warnIfCallingFromSystemProcess();
startActivity(intent, null);
}
Cette méthode a été d'appeler un autre méthode surchargée
//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, à cette méthode, nous pouvons voir l'exception de la précédente levée. Dans cet arrêt, sinon augmenter FLAG_ACTIVITY_NEW_TASK
le drapeau, il lancera une exception.
Regardez la méthode 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);
}
On peut voir dans Android7, relativement Android6, Jeté doivent également répondre à plusieurs autres conditions. Du point de vue startActivity Méthode d'application décrite ci-dessus, le type d'ensemble du paramètre options est nul, donc cette condition n'est pas satisfaite, aucune exception est levée.
Regardez la méthode 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);
}
Et comme Android7 aussi de déterminer les options, parce que les options est nulle, et par conséquent les conditions ne sont pas remplies, il ne sera pas jeter une exception.
Regardons à la méthode 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, pour les options de la null || avec le juge, puis ActivityOptions.fromBundle (options) .getLaunchTaskId () == -1, condition ou sera mis en place, ou jetteront une exception.
On peut voir dans Android7 et Android8, vous pouvez commencer directement à l'activité de l'application, sans qu'il soit nécessaire d'augmenter le drapeau FLAG_ACTIVITY_NEW_TASK.