为什么Android7.0和8.0上ApplicationContext.startActivty(...)不需要添加FLAG_ACTIVITY_NEW_TASK呢?

我们都知道在Android7.0之前,非Activity环境中(例如ApplicationContext)启动Activity需要添加FLAG_ACTIVITY_NEW_TASK标记位,才会正常启动Activity。因为非Activity的环境中并没有所谓的任务栈。

但是,最近在项目中发现,在Android7.0和8.0上并不需要添加FLAG_ACTIVITY_NEW_TASK标记位也可以正常启动Activity,在Android9.0的官方文档上又强制要求添加了。我们来看看官网是怎么说的:

在Android 9 中,你不能从非Activity环境中启动Activity,除非您传递Intent标志FLAG_ACTIVITY_NEW_TASK。如果您尝试在不传递此标志的情况下启动Activity,则该Activity不会启动,系统会在日志中输出一则消息。在Android 7.0之前,标志要求一直是期望的行为并被强制执行。Android7.0中的一个错误会临时阻止实施标志要求

看最后一句:“Android7.0中的一个错误会临时阻止实施标志要求”。难道是谷歌的一个BUG?我们来查看源码(frameworks/base/core/java/android/app/ContextImpl.java)看看:

  • 首先看看Android6.0上的写法
@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);
}
  • Android 7.0~Android8.0
 @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);
}

  • Android 9.0以上修复了这个问题,强制执行
@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);
}

在Android7.0-8.0 的中加入了options != null的判断,options是Bundle类型,正常启动Activity如果不传Bundle的时候,就不会抛出异常提醒。后来在Android9.0上又改成options == null了。

你可能感兴趣的:(Android开发总结)