上一篇文章粗略地介绍了一下关于Android中Task的基本知识。只是实践才是检验真理的唯一标准,所以。今天就来试验一下Task中的launchMode是否真的实现了文档所说的那样。

首先。定义三个Activity。MainActivity打开SecondActivity,SecondActivity打开ThirdActivity,例如以下所看到的:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlubWlhbnNoZW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlubWlhbnNoZW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

以下。我们定义Second Activity的Launch Mode。分别有以下几种情况:

1)Standard(即不定义)

首先是默认情况下。什么都不定义。例如以下:

    <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.lms.taskdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.lms.taskdemo.SecondActivity">
</activity>
<activity android:name="com.lms.taskdemo.ThirdActivity" >
</activity>
</application>

顺序打开三个Activity,看看结果:

04-21 15:23:28.373: V/com.lms.taskdemo(20473): Task : 70
04-21 15:23:28.373: V/com.lms.taskdemo(20473): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-21 15:23:28.373: V/com.lms.taskdemo(20473): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-21 15:23:28.373: V/com.lms.taskdemo(20473): Number of activities : 1
04-21 15:23:28.373: V/com.lms.taskdemo(20473): Number of running activities: 1
04-21 15:23:31.336: V/com.lms.taskdemo(20473): Task : 70
04-21 15:23:31.336: V/com.lms.taskdemo(20473): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-21 15:23:31.336: V/com.lms.taskdemo(20473): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
04-21 15:23:31.336: V/com.lms.taskdemo(20473): Number of activities : 2
04-21 15:23:31.336: V/com.lms.taskdemo(20473): Number of running activities: 2
04-21 15:23:33.999: V/com.lms.taskdemo(20473): Task : 70
04-21 15:23:33.999: V/com.lms.taskdemo(20473): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-21 15:23:33.999: V/com.lms.taskdemo(20473): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.ThirdActivity}
04-21 15:23:33.999: V/com.lms.taskdemo(20473): Number of activities : 3
04-21 15:23:33.999: V/com.lms.taskdemo(20473): Number of running activities: 3

能够看到三个Activity,都是在同个Task中的。

2)SingleTask

为Second Activity,定义其launch mode 为 “singleTask”。例如以下:

    <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.lms.taskdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.lms.taskdemo.SecondActivity"
android:launchMode="singleTask">
</activity>
<activity android:name="com.lms.taskdemo.ThirdActivity" >
</activity>
</application>

再来看看其结果,例如以下:

04-22 10:31:26.197: V/com.lms.taskdemo(924): Task : 162
04-22 10:31:26.217: V/com.lms.taskdemo(924): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-22 10:31:26.217: V/com.lms.taskdemo(924): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-22 10:31:26.217: V/com.lms.taskdemo(924): Number of activities : 1
04-22 10:31:26.217: V/com.lms.taskdemo(924): Number of running activities: 1
04-22 10:31:31.112: V/com.lms.taskdemo(924): Task : 162
04-22 10:31:31.112: V/com.lms.taskdemo(924): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-22 10:31:31.112: V/com.lms.taskdemo(924): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
04-22 10:31:31.112: V/com.lms.taskdemo(924): Number of activities : 2
04-22 10:31:31.112: V/com.lms.taskdemo(924): Number of running activities: 2
04-22 10:31:32.884: V/com.lms.taskdemo(924): Task : 162
04-22 10:31:32.884: V/com.lms.taskdemo(924): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-22 10:31:32.884: V/com.lms.taskdemo(924): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.ThirdActivity}
04-22 10:31:32.884: V/com.lms.taskdemo(924): Number of activities : 3
04-22 10:31:32.884: V/com.lms.taskdemo(924): Number of running activities: 2

发现尽管定义了SingleTask,但Second Activity和Main Activity还是在同一个Task中。这跟我们期望的不符。原因事实上在于,设置了launchMode = SingleTask启动的Activity,在启动的时候,会去查找跟它的taskAffinity同样的task,假设存在这样一个task,就在这个task中启动,假设不存在这样一个task,才创建一个新的task,并在新task中启动。

而在这里,由于没有为各个activity定义taskAffinity,那么默认的affinity值就是包名,那么几个Activity都是一样的,所以就在相同的Task中启动了。

3)Single Task Different Task Affinity

以下。我们分别为Main Activity和 Second Activity设置不同的TaskAffinity值,例如以下:

    <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.lms.taskdemo.MainActivity"
android:label="@string/app_name"
android:taskAffinity="com.lms.taskdemo.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.lms.taskdemo.SecondActivity"
android:launchMode="singleTask"
android:taskAffinity="com.lms.taskdemo.SecondActivity" >
</activity>
<activity android:name="com.lms.taskdemo.ThirdActivity" >
</activity>
</application>

以下是结果,启动的顺序是Main Activity->Second Activity->Main Activity -> Second Activity -> Third Activity

04-22 10:26:27.818: V/com.lms.taskdemo(32677): Task : 151
04-22 10:26:27.818: V/com.lms.taskdemo(32677): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-22 10:26:27.818: V/com.lms.taskdemo(32677): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-22 10:26:27.818: V/com.lms.taskdemo(32677): Number of activities : 1
04-22 10:26:27.818: V/com.lms.taskdemo(32677): Number of running activities: 1
04-22 10:26:31.362: V/com.lms.taskdemo(32677): Task : 152
04-22 10:26:31.362: V/com.lms.taskdemo(32677): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
04-22 10:26:31.362: V/com.lms.taskdemo(32677): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
04-22 10:26:31.372: V/com.lms.taskdemo(32677): Number of activities : 1
04-22 10:26:31.372: V/com.lms.taskdemo(32677): Number of running activities: 1
04-22 10:26:36.558: V/com.lms.taskdemo(32677): Task : 151
04-22 10:26:36.558: V/com.lms.taskdemo(32677): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-22 10:26:36.558: V/com.lms.taskdemo(32677): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-22 10:26:36.558: V/com.lms.taskdemo(32677): Number of activities : 1
04-22 10:26:36.558: V/com.lms.taskdemo(32677): Number of running activities: 1
04-22 10:26:39.401: V/com.lms.taskdemo(32677): Task : 153
04-22 10:26:39.401: V/com.lms.taskdemo(32677): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
04-22 10:26:39.401: V/com.lms.taskdemo(32677): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
04-22 10:26:39.401: V/com.lms.taskdemo(32677): Number of activities : 1
04-22 10:26:39.401: V/com.lms.taskdemo(32677): Number of running activities: 1
04-22 10:26:41.123: V/com.lms.taskdemo(32677): Task : 153
04-22 10:26:41.123: V/com.lms.taskdemo(32677): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
04-22 10:26:41.123: V/com.lms.taskdemo(32677): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.ThirdActivity}
04-22 10:26:41.123: V/com.lms.taskdemo(32677): Number of activities : 2
04-22 10:26:41.123: V/com.lms.taskdemo(32677): Number of running activities: 2

在上面的结果中,能够看到在启动Second Activity的时候,的确是在一个新的Task中启动了。Second Activity是这个Task中的根Activity。而由Second Activity启动的Third Activity也是在这个Task中启动的。

4)Single Instance

以下,我们再来看看Single Instance,相同的,先不定义Task Affinity。在AndroidManifest文件里定义例如以下:
    <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.lms.taskdemo.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.lms.taskdemo.SecondActivity"
android:launchMode="singleInstance">
</activity>
<activity android:name="com.lms.taskdemo.ThirdActivity" >
</activity>
</application>

以下是结果:

04-22 10:29:43.207: V/com.lms.taskdemo(682): Task : 159
04-22 10:29:43.207: V/com.lms.taskdemo(682): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-22 10:29:43.207: V/com.lms.taskdemo(682): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-22 10:29:43.207: V/com.lms.taskdemo(682): Number of activities : 1
04-22 10:29:43.207: V/com.lms.taskdemo(682): Number of running activities: 1
04-22 10:29:46.580: V/com.lms.taskdemo(682): Task : 160
04-22 10:29:46.580: V/com.lms.taskdemo(682): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
04-22 10:29:46.580: V/com.lms.taskdemo(682): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
04-22 10:29:46.580: V/com.lms.taskdemo(682): Number of activities : 1
04-22 10:29:46.580: V/com.lms.taskdemo(682): Number of running activities: 1
04-22 10:29:48.202: V/com.lms.taskdemo(682): Task : 159
04-22 10:29:48.202: V/com.lms.taskdemo(682): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-22 10:29:48.202: V/com.lms.taskdemo(682): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.ThirdActivity}
04-22 10:29:48.202: V/com.lms.taskdemo(682): Number of activities : 2
04-22 10:29:48.202: V/com.lms.taskdemo(682): Number of running activities: 1
04-22 10:29:57.112: V/com.lms.taskdemo(682): Task : 160
04-22 10:29:57.112: V/com.lms.taskdemo(682): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
04-22 10:29:57.112: V/com.lms.taskdemo(682): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
04-22 10:29:57.112: V/com.lms.taskdemo(682): Number of activities : 1
04-22 10:29:57.112: V/com.lms.taskdemo(682): Number of running activities: 1

能够看到启动Second Activity的时候,是在新的Task中,而当启动Third Activity的时候,它又跑回原来的Task中去了。这是跟文档描写叙述中符合的。的确是新建的Task中仅仅能有且仅有一个Activity。

而因为我们未定义Task Affinity,所以当启动Third Activity的时候。它就会去找同样Affinity的task,所以就会找到原来的Task。也即是说,假设定义了TaskAffinity的话,那以Third Activity就应该在新的Task中创建了,以下就来验证一下。


5)Single Instance Different Affinity

在配置文件里定义例如以下:
    <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.lms.taskdemo.MainActivity"
android:label="@string/app_name"
android:taskAffinity="com.lms.taskdemo.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.lms.taskdemo.SecondActivity"
android:launchMode="singleInstance"
android:taskAffinity="com.lms.taskdemo.SecondActivity" >
</activity>
<activity android:name="com.lms.taskdemo.ThirdActivity" >
</activity>
</application>

以下来看看结果,例如以下:

04-22 10:27:58.866: V/com.lms.taskdemo(391): Task : 155
04-22 10:27:58.866: V/com.lms.taskdemo(391): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-22 10:27:58.866: V/com.lms.taskdemo(391): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.MainActivity}
04-22 10:27:58.866: V/com.lms.taskdemo(391): Number of activities : 1
04-22 10:27:58.866: V/com.lms.taskdemo(391): Number of running activities: 1
04-22 10:28:00.677: V/com.lms.taskdemo(391): Task : 156
04-22 10:28:00.677: V/com.lms.taskdemo(391): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
04-22 10:28:00.677: V/com.lms.taskdemo(391): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.SecondActivity}
04-22 10:28:00.677: V/com.lms.taskdemo(391): Number of activities : 1
04-22 10:28:00.677: V/com.lms.taskdemo(391): Number of running activities: 1
04-22 10:28:02.309: V/com.lms.taskdemo(391): Task : 157
04-22 10:28:02.309: V/com.lms.taskdemo(391): Base activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.ThirdActivity}
04-22 10:28:02.309: V/com.lms.taskdemo(391): Current activity : ComponentInfo{com.lms.taskdemo/com.lms.taskdemo.ThirdActivity}
04-22 10:28:02.309: V/com.lms.taskdemo(391): Number of activities : 1
04-22 10:28:02.309: V/com.lms.taskdemo(391): Number of running activities: 1

果然。如我们期望的那样,都是在一个新的Task中启动当前的Activity的。


总结:

当使用Launch Mode 来改变系统默认的任务调度的时候。假设是用到Single Task或者Single Instance的时候,还要注意到Affinity的使用,要跟Affinity配合使用。可能才干达到我们期望中的效果。而Affinity,事实上是Android提供的一个表从属意义的參数,类似于一个Tag值,它表明当前Activity属于哪一个Tag,相当的Affinity值的Activity。假设不使用其它的标志,如Single Instance之类,那么都会在存在于同一个task中。普通情况下,我们并不定义Task
Affinity值。则其默认的值就是当前App的包名。

结束。

Android关于Task的一些实践之SingleTask, SingleInstance和TaskAffinity的更多相关文章

  1. Android的Task和Activity相关

    android:allowTaskReparenting    用来标记Activity能否从启动的Task移动到有着affinity的Task(当这个Task进入到前台时)——“true”,表示能移 ...

  2. Android和PHP开发最佳实践

    Android和PHP开发最佳实践 <Android和PHP开发最佳实践>基本信息作者: 黄隽实丛书名: 移动应用开发技术丛书出版社:机械工业出版社ISBN:9787111410508上架 ...

  3. Android学习笔记_50_(转 四种加载方式详解(standard singleTop singleTask singleInstance)

    Android之四种加载方式 (http://marshal.easymorse.com/archives/2950 图片) 在多Activity开发中,有可能是自己应用之间的Activity跳转,或 ...

  4. Android 四种加载方式详解(standard singleTop singleTask singleInstance) .

    Android之四种加载方式 (http://marshal.easymorse.com/archives/2950 图片) 在多Activity开发中,有可能是自己应用之间的Activity跳转,或 ...

  5. Google Developing for Android 三 - Performance最佳实践

    Google Developing for Android 三 - Performance最佳实践 发表于 2015-06-07   |   分类于 Android最佳实践 原文 Developing ...

  6. Google Developing for Android 二 - Memory 最佳实践 // lightSky‘Blog

    Google Developing for Android 二 - Memory 最佳实践   |   分类于 Android最佳实践 原文:Developing for Android, II Th ...

  7. Android 6.0 源代码编译实践

    http://www.judymax.com/archives/1087 Android 6.0 源代码编译实践 https://mirrors.tuna.tsinghua.edu.cn/help/A ...

  8. 美团外卖Android平台化的复用实践

    美团外卖平台化复用主要是指多端代码复用,正如美团外卖iOS多端复用的推动.支撑与思考文章所述,多端包含有两层意思:其一是相同业务的多入口,指美团外卖业务需要在美团外卖App(下文简称外卖App)和美团 ...

  9. Activity的启动模式全解standard,singleTop,singleTask,singleInstance

    在android中控制Activity的启动模式的属性主要控制两大功能: 1,控制activity 进入哪一个任务task 中,   有两种可能,进入启动task中,进入指定taskAffinity的 ...

随机推荐

  1. jsonP 现在360浏览器竟然阻止本机 jquery load一些html js什么的

    别的浏览器正常可以jquery.load本机文件,但是360浏览器不行了,缺德啊!! jsonP代码 index3.html <!DOCTYPE HTML PUBLIC "-//W3C ...

  2. C#委托与事件的关系(转载)

    1.C#中的事件和委托的作用?事件代表一个组件能够被关注的一种信号,比如你的大肠会向你发出想拉屎的信号,你就可以接收到上厕所.委托是可以把一个过程封装成变量进行传递并且执行的对象,比如你上蹲坑和上坐马 ...

  3. springboot Tomcat connector configured to listen on port 8081 failed to start.

    启动报 Tomcat connector configured to listen on port 8081 failed to start.   The port may already be in ...

  4. Delphi最简化异步选择TCP服务器

    网上Delphi的Socket服务器优良代码,实在少见,索性写个简化的异步Socket服务器,虽然代码较少,但却该有的都有了,使用的是异步选择WSAAsyncSelect,减少了编写线程的繁琐.可能会 ...

  5. ORACLE中的游标Cursor总结

    游标(Cursor):用来查询数据库,获取记录集合(结果集)的指针,可以让开发者一次访问一行结果集,在每条结果集上作操作. 游标可分为: 1.       静态游标:分为显式(explicit)游标和 ...

  6. CSU 2018年12月月赛 F(2218): Finding prime numbers

    Description xrdog has a number set. There are 95 numbers in this set. They all have something in com ...

  7. Shiro框架 (原理分析与简单实现)

    Shiro框架(原理分析与简单实现) 有兴趣的同学也可以阅读我之前分享的:Java权限管理(授权与认证)CRM权限管理   (PS : 这篇博客里面的实现方式没有使用框架,完全是手写的授权与认证,可以 ...

  8. bzoj 3224 NOI2004郁闷的出纳员

    NOI2004郁闷的出纳员 2013年12月26日6,1818 输入描述 Input Description 第一行有两个非负整数n和min.n表示下面有多少条命令,min表示工资下界. 接下来的n行 ...

  9. 【BZOJ4868】期末考试(整数三分)

    题意: 有n位同学,每位同学都参加了全部的m门课程的期末考试,都在焦急的等待成绩的公布.第i位同学希望在第ti天 或之前得知所.有.课程的成绩.如果在第ti天,有至少一门课程的成绩没有公布,他就会等待 ...

  10. c#中的委托和事件详细学习【分6页学习】

    原文发布时间为:2008-11-01 -- 来源于本人的百度文章 [由搬家工具导入] 在本文中我首先通过一个GreetingPeople的小程序向大家介绍了委托的概念、委托用来做什么,随后又引出了事件 ...