FLAG_ACTIVITY_NEW_TASK使用场景及原理简析

FLAG_ACTIVITY_NEW_TASK使用场景及原理简析在非Activity(比如Service,BroadcastReceiver)中startActivity需要添加flagIntent.FLAG_ACTIVITY_NEW_TASK。否则会报Crash:android.util.AndroidRuntimeException:CallingstartActivity()fromoutsideofanActivitycontext…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

在非Activity(比如Service,BroadcastReceiver)中startActivity需要添加flag Intent.FLAG_ACTIVITY_NEW_TASK。否则会报Crash:android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.

原文地址

以下Service为前提说明

简单来说:

每个Activity启动都需要依赖一个Task栈,在Service中启动Activity时一般使用ApplicationContext,它没自己的Task栈,(Activity有自己的栈,但Service中保存Activity实例容易造成内存泄漏)。

补充说明:

在MainActivity启动时,系统自动创建了App的Task栈,并且taskAffinity默认为包名:com.test.haha
以下为三种情况:

  • 情况1:只在Activity添加FLAG_ACTIVITY_NEW_TASK
    使用Task栈和MainActivity的Task栈是一样的,因为没有在AndroidMainfest添加taskAffinity,那么taskAffinity的默认值为包名即com.test.haha

  • 情况2:只在AndroidMainfest添加taskAffinity 不添加flag,无效

  • 情况3:同时在Activity添加FLAG_ACTIVITY_NEW_TASK 和 在AndroidMainfest添加taskAffinity(若指定为包名,则转到情况1)

    则创建新的Task栈,行为同启动模式singleTask。

可以使用adb shell dumpsys activity activities 或者 打印getTaskId()来验证。

请参考Google文档:任务和返回栈

注意: 当调用者需要从启动的Activity获取返回结果时,不能使用此标志。即使用startActivityForResult()要注意,没有返回值的。(见官网注释最后一句)

官网注释:

FLAG_ACTIVITY_NEW_TASK

If set, this activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move to. Tasks can be moved to the foreground and background; all of the activities inside of a particular task always remain in the same order. See Tasks and Back Stack for more information about tasks.

This flag is generally used by activities that want to present a “launcher” style behavior: they give the user a list of separate things that can be done, which otherwise run completely independently of the activity launching them.

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.

This flag can not be used when the caller is requesting a result from the activity being launched.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/185379.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • sigterm信号_一文吃透 PHP 进程信号处理

    sigterm信号_一文吃透 PHP 进程信号处理背景前两周老大给安排了一个任务,写一个监听信号的包。因为我司的项目是运行在容器里边的,每次上线,需要重新打包镜像,然后启动。在重新打包之前,Dokcer会先给容器发送一个信号,然后等待一段超时时间(默认10s)后,再发送SIGKILL信号来终止容器现在有一种情况,容器中有一个常驻进程,该常驻进程的任务是不断的消费队列里的消息。假设现在要上线,需要关杀掉容器,Docker给容器里跑的常驻进程发送一个…

    2025年8月12日
    3
  • Same Tree

    Same Tree

    2022年1月25日
    57
  • Egret 位图_左上格横中线

    Egret 位图_左上格横中线1、准备切割好的图片文件2、打开TextureMerger软件,点击BitmapFont3、将艺术字拖进软件中4、点击导出,导出成功后会生成一个fnt文件和一个png文件5、创建位图对象//文本modulegame{exportclasstextDemoextendsegret.DisplayObjectContainer{…

    2025年7月1日
    4
  • Pytest(6)重复运行用例pytest-repeat「建议收藏」

    Pytest(6)重复运行用例pytest-repeat「建议收藏」前言平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来。自动化运行用例时候,也会出现偶然的bug,可以针对单个用例,

    2022年7月31日
    5
  • 2、Java基础02 – 【命令行运行HelloWorld】[通俗易懂]

    2、Java基础02 – 【命令行运行HelloWorld】[通俗易懂]操作步骤:1、新建一个文件夹(可以命名为Java)2、新建一个.txt文本文件,在文件中输入如下代码:publicclassHelloWorld{publicstaticvoidmain(String[]args){System.out.println(“helloworld”);}}3、重命名将文件名改为HelloWorld.java,并创建第一个java源文件4、编译.java文件是java的源文件,但是不能直接运行,必须先被编译成为.class文件才能够

    2022年5月20日
    40
  • Storm On YARN带来的好处

    Storm On YARN带来的好处

    2021年12月17日
    67

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号