作为一只安卓小白,还是愿意和大家分享我的探索旅程,那么就让我们先来一点来自Android launchMode的温馨提示吧~(≧▽≦)/~。
“现在我的手中有一摞牌,但是我让你看到的只有一张。”
“其他牌去哪了呢?”
“其他张牌都被最上面这张牌挡住了呀。”
上面的这段话,很形象地解释了acitivity和任务栈的关系。
接下来我们首先了解一下任务——Task。
而在以上情况下,我们的默认启动模式都是无法满足的。
那么问题来了。。。启动模式哪家强(=@__@=)?
接下来,就让我们走近Android的4种启动模式吧~
来,让我们一起做个实验吧
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xixi.standardlaunchmode" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ActivityA"
android:label="@string/app_name" />
<activity
android:name=".ActivityB"
android:taskAffinity="com.other" />
</application>
</manifest>
实验过程
实验探析:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xixi.standardlaunchmode" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ActivityA"
android:label="@string/app_name" />
<activity
android:name=".ActivityB"
android:taskAffinity="com.other" />
</application>
</manifest>
首先 我们依然先看下日志:
接下来,进一步查看下任务栈:
性能总结
接下来,我们需要进入与前两种模式完全不一样的啦,稍事休息:
AV8D,R U ready?嘿儿未够!
对于这个磨人的“小妖精”,官方文档中是这么描述的:
The system creates a new task and instantiates the activity at the root of the new task.
However, if an instance of the activity already exists in a separate task,`
the system routes the intent to the existing instance through a call to its onNewIntent() method,rather than creating a new instance. Only one instance of the activity can exist at a time.
而事实真的是这样么 还是先做实验吧
<activity android:name=".ActivityA" android:launchMode="singleTask" />
<activity android:name=".ActivityB" />
<activity android:name=".ActivityC" android:taskAffinity="com.others" />
<activity android:name=".ActivityA" />
<activity android:name=".ActivityB" android:launchMode="singleTask" />
<activity android:name=".ActivityC" android:taskAffinity="com.others" />
任务栈记录
再次启动B后:
小结:
通过以上2个实验,我们可以看到:
实验配置:
与实验1相同;
再次启动A后:
通过这个实验,我们发现ActivityA还在task 25中,并没有在新的task中实例化,并且再次启动它时,为了把它置于栈顶,原来在它上面的activity也被结束了。
同SingleTask一样有且只有一个实例 那么区别在哪捏?
<activity android:name=".ActivityA" android:launchMode="singleInstance" />
<activity android:name=".ActivityB" />
<activity android:name=".ActivityC" android:taskAffinity="com.others" />
实验探析
再次启动A后:
那么日志是啥样的呢?
小结:
<activity android:name=".ActivityA" />
<activity android:name=".ActivityB" android:launchMode="singleInstance" />
<activity android:name=".ActivityC" android:taskAffinity="com.others" />
实验过程
再次启动B后:
性能总结
foreach task in the tasks{
if(hasSingleInstanceActivity) {
task.isUnique = true;
task.activityCapacity = 1;
singleInstance.isUnique = true;
acitvity.isRootActivity = true;
}
}
http://www.slideshare.net/RanNachmany/manipulating-android-tasks-and-back-stack
http://developer.android.com/guide/components/tasks-and-back-stack.html
http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en
这里还有一个中文版哈Acitivity启动模式图文详解
http://blog.csdn.net/guolin_blog/article/details/41087993
http://blog.csdn.net/luoshengyang/article/details/6714543
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/chenxin_003/article/details/47684173