为什么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了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ltym2014

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值