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

小编 2026-07-02 阅读:499 评论:0
我们都知道在Android7.0之前,非Activity环境中(例如ApplicationContext)启动Activity需要添加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了。

版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表