Activity的5种启动模式详解(新增singleInstancePerTask类型)

这篇具有很好参考价值的文章主要介绍了Activity的5种启动模式详解(新增singleInstancePerTask类型)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

看到这个标题的时候,也许你会认为我写错了,Activity不是只有四种启动模式吗?分别为startard,singleTop,singleTask,singleInstance这四种。

一般来说是四种,但是android12的时候新加入了singleInstancePerTask类型,所以就有5种了。

介绍这五种类型之前,我们先略微介绍一下这五种类型在源码中的定义。

首先,我们先看一下官方注释的位置下,这五种类型的注释在attrs_manifest.xml文件中:

<attr name="launchMode">
        <!-- The default mode, which will usually create a new instance of
             the activity when it is started, though this behavior may change
             with the introduction of other options such as
             {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK
             Intent.FLAG_ACTIVITY_NEW_TASK}. -->
        <enum name="standard" value="0" />
        <!-- If, when starting the activity, there is already an
            instance of the same activity class in the foreground that is
            interacting with the user, then
            re-use that instance.  This existing instance will receive a call to
            {@link android.app.Activity#onNewIntent Activity.onNewIntent()} with
            the new Intent that is being started. -->
        <enum name="singleTop" value="1" />
        <!-- If, when starting the activity, there is already a task running
            that starts with this activity, then instead of starting a new
            instance the current task is brought to the front.  The existing
            instance will receive a call to {@link android.app.Activity#onNewIntent
            Activity.onNewIntent()}
            with the new Intent that is being started, and with the
            {@link android.content.Intent#FLAG_ACTIVITY_BROUGHT_TO_FRONT
            Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT} flag set.  This is a superset
            of the singleTop mode, where if there is already an instance
            of the activity being started at the top of the stack, it will
            receive the Intent as described there (without the
            FLAG_ACTIVITY_BROUGHT_TO_FRONT flag set).  See the
            <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
            Stack</a> document for more details about tasks.-->
        <enum name="singleTask" value="2" />
        <!-- Only allow one instance of this activity to ever be
            running.  This activity gets a unique task with only itself running
            in it; if it is ever launched again with the same Intent, then that
            task will be brought forward and its
            {@link android.app.Activity#onNewIntent Activity.onNewIntent()}
            method called.  If this
            activity tries to start a new activity, that new activity will be
            launched in a separate task.  See the
            <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
            Stack</a> document for more details about tasks.-->
        <enum name="singleInstance" value="3" />
        <!-- The activity can only be running as the root activity of the task, the first activity
            that created the task, and therefore there will only be one instance of this activity
            in a task. In constrast to the {@code singleTask} launch mode, this activity can be
            started in multiple instances in different tasks if the
            {@code FLAG_ACTIVITY_MULTIPLE_TASK} or {@code FLAG_ACTIVITY_NEW_DOCUMENT} is set.-->
        <enum name="singleInstancePerTask" value="4" />
    </attr>

其次,在看一下这五种模式在Java文件中的定义,在ActivityInfo类中,如下:

    /**
     * Constant corresponding to <code>standard</code> in
     * the {@link android.R.attr#launchMode} attribute.
     /
    public static final int LAUNCH_MULTIPLE = 0;
    /*
     * Constant corresponding to <code>singleTop</code> in
     * the {@link android.R.attr#launchMode} attribute.
     /
    public static final int LAUNCH_SINGLE_TOP = 1;
    /*
     * Constant corresponding to <code>singleTask</code> in
     * the {@link android.R.attr#launchMode} attribute.
     /
    public static final int LAUNCH_SINGLE_TASK = 2;
    /*
     * Constant corresponding to <code>singleInstance</code> in
     * the {@link android.R.attr#launchMode} attribute.
     /
    public static final int LAUNCH_SINGLE_INSTANCE = 3;
    /*
     * Constant corresponding to <code>singleInstancePerTask</code> in
     * the {@link android.R.attr#launchMode} attribute.
     */
    public static final int LAUNCH_SINGLE_INSTANCE_PER_TASK = 4;

接下来,我们就依次讲解这五种类型的定义和作用。

一.standard类型

首先讲解standard类型,这也是一个最基本最常用的一种类型。

官方解释

先看官方注释中的描述:

        <!-- The default mode, which will usually create a new instance of
             the activity when it is started, though this behavior may change
             with the introduction of other options such as
             {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK
             Intent.FLAG_ACTIVITY_NEW_TASK}. -->

翻译后:

默认的类型,每次使用时通常创建一个新的活动对象,虽然这样的行为会因为引入其它参数配置而发生改变,比如Intent.FLAG_ACTIVITY_NEW_TASK。

实验验证

我们再来通过实验进行验证,创建三个Activity,分别为Test1Activity,Test2Activity,Test3Activity,如下:

        <activity android:name="com.xt.client.activitys.test.Test1Activity" />
        <activity android:name="com.xt.client.activitys.test.Test2Activity" />
        <activity android:name="com.xt.client.activitys.test.Test3Activity" />

然后我们在代码中设置如下跳转顺序:Test1->Test2->Test3->Test1

接下来就是实验,按照上面流程跳转后,使用adb命令捕获当前Activity的栈状态如下:

adb命令:adb shell dumpsys activity activities

      Application tokens in top down Z order:
      * Task{54bb09b #190 type=standard A=10234:com.xt.client U=0 visible=true visibleRequested=true mode=fullscreen translucent=false sz=4}
        bounds=[0,0][1080,2340]
        * ActivityRecord{75f3f53 u0 com.xt.client/.activitys.test.Test1Activity} t190}
        * ActivityRecord{f91f056 u0 com.xt.client/.activitys.test.Test3Activity} t191}
        * ActivityRecord{777cf91 u0 com.xt.client/.activitys.test.Test2Activity} t190}
        * ActivityRecord{83d9d17 u0 com.xt.client/.activitys.test.Test1Activity} t190}

整个流程如下图所示,Test3跳转Test1的时候,会直接在Test3上面入栈一个新的Test1。所以Task中有4条ActivityRecrod,从下往上的顺序为:Test1,Test2,Test3,Test1。

Activity的5种启动模式详解(新增singleInstancePerTask类型)

总结:

首先这里我们就不验证添加参数的类型了。

standard类型的启动方式,是不断的往一个Task栈上面添加新的Activity,哪怕有同样的Activity,也会重新创建一个添加到栈里面。

二.singleInstance类型

为什么把singleInstance拿到前面来说呢?因为后面singleTop类型中的验证会涉及到singleInstance类型的知识点,所以就拿到前面提前来讲了。

官方解释

<!-- Only allow one instance of this activity to ever be
    running.  This activity gets a unique task with only itself running
    in it; if it is ever launched again with the same Intent, then that
    task will be brought forward and its
    {@link android.app.Activity#onNewIntent Activity.onNewIntent()}
    method called.  If this
    activity tries to start a new activity, that new activity will be
    launched in a separate task.  See the
    <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
    Stack</a> document for more details about tasks.-->

翻译后:

只允许一个Activity的实例运行。这个Activity会有唯一的task栈只允许它自身在其中运行,每次被加载的时候都被会把这个栈带到前台,同时会调用Activity的onNewIntent方法进行通知。如果这个Activity尝试去启动一个新的Activity,那么新创建的Activity和当前Activity必须是不同的Task栈。

实验验证

我们仍然是三个Activity,其中Test2Activity设置为singleInstance的模式,

        <activity android:name="com.xt.client.activitys.test.Test1Activity" />
        <activity
            android:name="com.xt.client.activitys.test.Test2Activity"
            android:launchMode="singleInstance" />
        <activity android:name="com.xt.client.activitys.test.Test3Activity" />

设置跳转顺序: Test1->Test2->Test3->Test1->Test2->Test3

执行操作,结果如下,我们可以发现Test2单独占用一个Task

      Application tokens in top down Z order:
      * Task{40cbc68 #196 type=standard A=10234:com.xt.client U=0 visible=true visibleRequested=true mode=fullscreen translucent=false sz=5}
        bounds=[0,0][1080,2340]
        * ActivityRecord{cd9b41f u0 com.xt.client/.activitys.test.Test3Activity} t196}
        * ActivityRecord{9e73f05 u0 com.xt.client/.activitys.test.Test1Activity} t196}
        * ActivityRecord{8fcaaee u0 com.xt.client/.activitys.test.Test3Activity} t196}
        * ActivityRecord{5e3a630 u0 com.xt.client/.activitys.test.Test1Activity} t196}
        * ActivityRecord{9768990 u0 com.xt.client/.MainActivity} t196}
      * Task{17dfc90 #197 type=standard A=10234:com.xt.client U=0 visible=false visibleRequested=false mode=fullscreen translucent=true sz=1}
        bounds=[0,0][1080,2340]
        * ActivityRecord{e147b53 u0 com.xt.client/.activitys.test.Test2Activity} t197}

我们发现区别于第一种类型,这种类型下会有两个Task栈,这两个Task的taskAffinity是一样的。

Test2启动Test3的时候,流程图如下所示。首先前台任务栈从Task2切换到了Task1,Task1中也增加了一条Test3的Activity记录,此时显示在前台的就是Test3了。

Activity的5种启动模式详解(新增singleInstancePerTask类型)

总结:

singleInstance类型的启动方式中,

如果不存在包含目标Activity的栈,则创建一个新的Task,这个Task中是目标Activity所独有的,并且只会创建一次,后续如果在启动其它的Activity,这些新的Acitivty属于其parent的task栈。

如果存在包含目标Activity的栈,则直接把包含目标Activity的Task栈挪到前台进行显示。

三.singleTop类型

singleTop相对于startard多了一个复用的概念,就是说某些场景下,会复用已有的Activity而不是每次都新建。

官方解释

        <!-- If, when starting the activity, there is already an
            instance of the same activity class in the foreground that is
            interacting with the user, then
            re-use that instance.  This existing instance will receive a call to
            {@link android.app.Activity#onNewIntent Activity.onNewIntent()} with
            the new Intent that is being started. -->

翻译后:

在启动Activity的时候,如果前台中存在一个相同Activity类的实例与用户交互,则直接使用这个实例。这个存在的实例在启动的时候会通过onNewIntent方法收到通知。

实验验证

这里为了体现singleTop的作用,我们做一个对比实验。

首先,我们设置Test3为singleInstance模式,其他的都是默认的standard模式。

        <activity android:name="com.xt.client.activitys.test.Test1Activity" />
        <activity android:name="com.xt.client.activitys.test.Test2Activity" />
        <activity
            android:name="com.xt.client.activitys.test.Test3Activity"
            android:launchMode="singleInstance" />

然后设置跳转顺序:Test1->Test2->Test3->Test2

结果如下:

      Application tokens in top down Z order:
      * Task{54bb09b #190 type=standard A=10234:com.xt.client U=0 visible=true visibleRequested=true mode=fullscreen translucent=false sz=4}
        bounds=[0,0][1080,2340]
        * ActivityRecord{75f3f53 u0 com.xt.client/.activitys.test.Test2Activity} t190}
        * ActivityRecord{777cf91 u0 com.xt.client/.activitys.test.Test2Activity} t190}
        * ActivityRecord{83d9d17 u0 com.xt.client/.activitys.test.Test1Activity} t190}
        * ActivityRecord{6979f3f u0 com.xt.client/.MainActivity} t190}
      * Task{e1b8d7 #191 type=standard A=10234:com.xt.client U=0 visible=false visibleRequested=false mode=fullscreen translucent=true sz=1}
        bounds=[0,0][1080,2340]
        * ActivityRecord{f91f056 u0 com.xt.client/.activitys.test.Test3Activity} t191}

我们可以看到有两个Task栈,Task1中有3个Activity记录,分别是Test1,Test2,Test2。而Task2中则只有一个Test3的记录。

此时,我们做一个改动,把Test2设置为singleTop类型,如下:

        <activity android:name="com.xt.client.activitys.test.Test1Activity" />
        <activity android:name="com.xt.client.activitys.test.Test2Activity"
            android:launchMode="singleTop" />
        <activity
            android:name="com.xt.client.activitys.test.Test3Activity"
            android:launchMode="singleInstance" />

同样的跳转顺序:Test1->Test2->Test3->Test2

结果如下:

      Application tokens in top down Z order:
      * Task{54bb09b #190 type=standard A=10234:com.xt.client U=0 visible=true visibleRequested=true mode=fullscreen translucent=false sz=4}
        bounds=[0,0][1080,2340]
        * ActivityRecord{777cf91 u0 com.xt.client/.activitys.test.Test2Activity} t190}
        * ActivityRecord{83d9d17 u0 com.xt.client/.activitys.test.Test1Activity} t190}
      * Task{e1b8d7 #191 type=standard A=10234:com.xt.client U=0 visible=false visibleRequested=false mode=fullscreen translucent=true sz=1}
        bounds=[0,0][1080,2340]
        * ActivityRecord{f91f056 u0 com.xt.client/.activitys.test.Test3Activity} t191}

我们发现,只有Task2仍然只有一条Test3的记录,但是Task1中,只有两条记录了,分别为Test1和Test2。相对于上一次实验,这次少了一个Test2的记录。

Test3跳转Test2的流程图如下,前台任务栈从Task1切换到了Task2,并且Test2没有新建,而是直接复用栈顶的实例对象。

Activity的5种启动模式详解(新增singleInstancePerTask类型)

总结:

总结一下,singleTop类型的启动方式,当从别的Task任务栈切换回当前任务栈时,如果栈顶实例就是同样的Activity,则直接进行复用,并且通过onNewIntent方法进行通知。

四.singleTask类型

singleTask类型其实就是singleTop类型的增强版,我们来看下他们的区别。

官方解释

        <!-- If, when starting the activity, there is already a task running
            that starts with this activity, then instead of starting a new
            instance the current task is brought to the front.  The existing
            instance will receive a call to {@link android.app.Activity#onNewIntent
            Activity.onNewIntent()}
            with the new Intent that is being started, and with the
            {@link android.content.Intent#FLAG_ACTIVITY_BROUGHT_TO_FRONT
            Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT} flag set.  This is a superset
            of the singleTop mode, where if there is already an instance
            of the activity being started at the top of the stack, it will
            receive the Intent as described there (without the
            FLAG_ACTIVITY_BROUGHT_TO_FRONT flag set).  See the
            <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
            Stack</a> document for more details about tasks.-->

翻译后:

在启动Activity的时候,如果栈中存在一个这样的Activity,会通过在当前栈中把Activity对象带到前台的方式来替代新创建一个对象。启动的时候那个已存在的实例对象中onNewIntent方法会收到通知,并且会有Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT的标记。这是singleTop类型的扩展,如果目标Activity就处于栈顶部,则不会有FLAG_ACTIVITY_BROUGHT_TO_FRONT标记。

实验验证

我们采取和singeTop类似的方式,仍然进行对比实验。

仍然是三个Activity,其中Test1Activity设置为singleTask的模式,如下:

        <activity android:name="com.xt.client.activitys.test.Test1Activity" />
        <activity android:name="com.xt.client.activitys.test.Test2Activity" />
        <activity android:name="com.xt.client.activitys.test.Test3Activity"
            android:launchMode="singleInstance" />

区别于singeTop中的方式,我们这里设置Test3跳转回Test1。

设置如下跳转顺序:Test1->Test2->Test3->Test1

进行操作,使用adb命令捕获栈结构如下:

      Application tokens in top down Z order:
      * Task{d44dd11 #198 type=standard A=10234:com.xt.client U=0 visible=true visibleRequested=true mode=fullscreen translucent=false sz=4}
        bounds=[0,0][1080,2340]
        * ActivityRecord{5902bb5 u0 com.xt.client/.activitys.test.Test1Activity} t198}
        * ActivityRecord{46c4832 u0 com.xt.client/.activitys.test.Test2Activity} t198}
        * ActivityRecord{6e48322 u0 com.xt.client/.activitys.test.Test1Activity} t198}
      * Task{9bdbdf4 #199 type=standard A=10234:com.xt.client U=0 visible=false visibleRequested=false mode=fullscreen translucent=true sz=1}
        bounds=[0,0][1080,2340]
        * ActivityRecord{f0693c7 u0 com.xt.client/.activitys.test.Test3Activity} t199}

Task1中有Test1,Test2,Test1三条记录,Task2中只有Test3一条记录。

我们再把Test1改为singleTask类型,如下:

        <activity android:name="com.xt.client.activitys.test.Test1Activity" 
            android:launchMode="singleTask" />
        <activity android:name="com.xt.client.activitys.test.Test2Activity" />
        <activity android:name="com.xt.client.activitys.test.Test3Activity"
            android:launchMode="singleInstance" />

同样的跳转顺序:Test1->Test2->Test3->Test1,再次进行实验,结果如下:

      Application tokens in top down Z order:
      * Task{9de4b6a #200 type=standard A=10234:com.xt.client U=0 visible=true visibleRequested=true mode=fullscreen translucent=false sz=2}
        bounds=[0,0][1080,2340]
        * ActivityRecord{4bdcfd7 u0 com.xt.client/.activitys.test.Test1Activity} t200}
      * Task{af6f1f0 #201 type=standard A=10234:com.xt.client U=0 visible=false visibleRequested=false mode=fullscreen translucent=true sz=1}
        bounds=[0,0][1080,2340]
        * ActivityRecord{66c6a33 u0 com.xt.client/.activitys.test.Test3Activity} t201}

这时候我们发现区别于上一个实验,这时Task1中只有一条Test1的记录了。

整个流程如下图所示,Test3中启动Test1的时候,前台任务栈从Task2切换到Task1,并且直接把Task1中Test2记录进行出栈操作,此时显示在前台的就是Test1了。

Activity的5种启动模式详解(新增singleInstancePerTask类型)

总结:

使用singleTask类型后,如果发现目标任务栈Task中存在相同的Activity实例时,则会出栈该实例上面的所有其它类型的实例,从而让该实例展示到前台。

六.singleInstancePerTask

singleInstancePerTask由其名可知,是singleInstance类型的增强版,其实在我看来,更像是singleInstance和singleTask的结合体,接下来我们来看下区别。

官方解释

<!-- The activity can only be running as the root activity of the task, the first activity
    that created the task, and therefore there will only be one instance of this activity
    in a task. In constrast to the {@code singleTask} launch mode, this activity can be
    started in multiple instances in different tasks if the
    {@code FLAG_ACTIVITY_MULTIPLE_TASK} or {@code FLAG_ACTIVITY_NEW_DOCUMENT} is set.-->

翻译后:

Activity只能作为Task栈的根结点(第一个被创建)运行,因此栈中只可能有一个该Activity的实例。对比singleTask类型,如果设置了FLAG_ACTIVITY_MULTIPLE_TASK标记该Activity可以在不同的栈生成多个不同的实例。

实验验证

设置三个Activity,其中Test3Activity设置为singleInstancePerTask的模式,如下:

<activity android:name="com.xt.client.activitys.test.Test1Activity" />
<activity android:name="com.xt.client.activitys.test.Test2Activity" />
<activity
    android:name="com.xt.client.activitys.test.Test3Activity"
    android:launchMode="singleInstancePerTask" />

设置如下跳转顺序:Test1->Test2->Test3->Test1->Test2->Test3->Test1

进行操作,使用adb命令捕获栈结构如下:

      Application tokens in top down Z order:
      * Task{e77ae1e #206 type=standard A=10234:com.xt.client U=0 visible=true visibleRequested=true mode=fullscreen translucent=false sz=2}
        bounds=[0,0][1080,2340]
        * ActivityRecord{a818f11 u0 com.xt.client/.activitys.test.Test1Activity} t206}
        * ActivityRecord{632e259 u0 com.xt.client/.activitys.test.Test3Activity} t206}
      * Task{6ef6ec0 #205 type=standard A=10234:com.xt.client U=0 visible=false visibleRequested=false mode=fullscreen translucent=true sz=3}
        bounds=[0,0][1080,2340]
        * ActivityRecord{9918e35 u0 com.xt.client/.activitys.test.Test2Activity} t205}
        * ActivityRecord{25df40c u0 com.xt.client/.activitys.test.Test1Activity} t205}

可以看到,虽然启动了两次Test3,但是仍然只有两个Task栈,可以证明singleInstancePerTask类型是栈单例的,一个被声明的Activity只会拥有唯一的一个栈(不设置标记的前提下)。

如下图所示,区别于singleInstance类型的是,在这个归属Test3的栈中,是可以放入其它的Activity的,比如这里的Test1。

Activity的5种启动模式详解(新增singleInstancePerTask类型)

总结

singleInstancePerTask类型的启动方式中,

如果不存在包含目标Activity的栈,则创建一个新的Task,这个Task中是目标Activity所独有的,并且只会创建一次,后续如果在启动其它的Activity,这些新的Acitivty仍然当前的task栈。

如果存在包含目标Activity的栈,则把包含目标Activity的Task栈挪到前台,并且把该栈中目标Activity上面的所有Activity进行出栈操作,从而实现目标Activity显示在前台的效果。

当然,如官方文档所描述,如果启动singleInstancePerTask类型的Activity时,同时设置FLAG_ACTIVITY_MULTIPLE_TASK标记,应该是可以启动多个包含目标Activity的Task栈的,这里就不做实验了。

扩展性问题:

问题1:如果一个Task中的Activity全部出栈,会退到哪里?

答:假设Task1启动了Task2,那么Task2中的Activity如果全部出栈,就会把Task1推到前台,Task1中栈顶的Activity就会被显示。

我们以singleInstance类型中的流程为例,假设启动流程如下:

MainActivity(Task1)->Test1(Task2)->Test2(Task2)->Test3(Task3)->Test1(Task2)->Test2(Task3)->Test3(Task2)。

最终Test3属于Task2的栈,所以Task2中的4个Activity记录全部出栈后,会返回Task1的MainActivity,而不是Test2。

问题2:如何区分是首次创建还是从栈中返回的?

答:这个注释中就告诉我们了,栈中复用的回调用onNewIntent方法。

问题3:如何区分是从栈顶复用还是栈中复用的?

答:首先解释下栈顶复用还是栈中复用。如下图所示,如果启动的是Test3,就是栈顶复用,如果启动的是Test2或者Test1,那么就是栈中复用。

Activity的5种启动模式详解(新增singleInstancePerTask类型)

这个其实官方文档的注释中也告诉我们结果了,如果是栈中复用,则onNewIntent方法中,其flags参数中会包含Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT标记,可以通过这个来区别。文章来源地址https://www.toymoban.com/news/detail-420496.html

到了这里,关于Activity的5种启动模式详解(新增singleInstancePerTask类型)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android Activity启动流程一:从Intent到Activity创建

    关于作者:CSDN内容合伙人、技术专家, 从零开始做日活千万级APP。 专注于分享各领域原创系列文章 ,擅长java后端、移动开发、人工智能等,希望大家多多支持。 学习前,建议有相关知识储备: 【Android 基础】 应用(Application)启动流程 通过本文你可以学习到Activity启动流

    2024年02月10日
    浏览(35)
  • Activity启动流程分析

    点击桌面App图标,Launcher进程采用Binder IPC向system_server进程发起startActivity请求 system_server进程接收到请求后,向zygote进程发送创建进程的请求; Zygote进程fork出新的子进程,即App进程 App进程,通过Binder IPC向sytem_server进程发起attachApplication请求; system_server进程在收到请求后,进

    2024年02月09日
    浏览(47)
  • Activity启动流程概述

    Activity的启动过程,我们可以从 Context 的 startActivity 说起,其实现是 ContextImpl 的 startActivity (内部调用 startActivityForResult ),然后内部会通过 Instrumentation 来尝试启动 Activity ,这是一个 跨进程过程 ,它会调用ams的 startActivity 方法,当 ams校验完activity的合法性 后,会通过 Ap

    2024年02月11日
    浏览(25)
  • Android 后台启动Activity适配

    在Android 9及以下版本,后台启动Activity相对自由,但是如果在Activity上下文之外启动Activity会有限制。 所以此时需要给intent添加flag:FLAG_ACTIVITY_NEW_TASK。 在Android版本10及以后版本, 引入了后台执行限制,限制了应用在后台执行操作的能力。非核心任务的后台启动 Activity 可能会

    2024年02月10日
    浏览(30)
  • Activity详细解释(生命周期、以各种方式启动Activity、状态保存,等完全退出)

    简单的说:Activity或者悬浮于其它窗体上的交互界面。 在一个应用程序中通常由多个Activity构成。都会在Manifest.xml中指定一个主的Activity,例如以下设置 actionandroid:name=”android.intent.action.MAIN” / 当程序第一次执行时用户就会看这个Activity,这个Activity能够通过启动其它的Activi

    2024年02月04日
    浏览(37)
  • Activity 的启动流程(Android 13)

    Activity 的启动过程分为两种:一种是普通 Activity 的启动过程,另一种是根 Activity 的启动过程。普通 Activity 指的是除应用程序启动的第一个 Activity 之外的其他 Activity。 根 Activity 指的是应用程序启动的第一个 Activity,因此,根 Activity 的启动过程一般情况下也可以理解为应用程

    2024年02月11日
    浏览(34)
  • android 如何从后台启动activity

            从事开发android的工程师都知道 从android 10之后的系统是不允许从后台启动activity,这就导致很多功能实现不了,比如android 10的手机,app在后台,收到一条推送,是可以把自己拉起到前台,从而提高用户时长。         那么,有没有办法解决呢?所谓,上有政策,下游

    2024年02月10日
    浏览(32)
  • STM32启动模式详解

    一个单片机最小系统由电源、晶振、下载电路、BOOT电路、和复位电路组成。少一个单片机都启动不了。 BOOT电路本质上是单片机的引脚,在ESP32中BOOT电路被称为称为GPIO_Strapping管脚,引脚图如下: 作用: BOOT电路的作用是用于确定单片机的启动模式(启动模式后面会有说明)

    2024年02月10日
    浏览(30)
  • adnroid 11. 0 Activity启动流程图解

    从Launcher到ActivityTaskManager 从ActivityTaskManagerService 到 ApplicationThread 从ApplicationThread到onCreate

    2024年02月15日
    浏览(34)
  • Android Activity 启动流程 二:setContentView

    关于作者:CSDN内容合伙人、技术专家, 从零开始做日活千万级APP。 专注于分享各领域原创系列文章 ,擅长java后端、移动开发、商业变现、人工智能等,希望大家多多支持。 接 - 上 篇,Activity创建后,还只是调用了onCreate方法,页面并没有展示出来,还需要调用setContentVie

    2024年02月10日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包