迁移源地址为:《Activity四种启动模式》
置顶《转载》— 原文链接:http://www.cnblogs.com/meizixiong/archive/2013/07/03/3170591.html<div>
</div><div>
</div><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">一、启动模式介绍</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> 启动模式简单地说就是Activity启动时的策略,在AndroidManifest.xml中的标签的android:launchMode属性设置;</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> 启动模式有4种,分别为standard、singleTop、singleTask、singleInstance;</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">讲解启动模式之前,有必要先讲解一下“任务栈”的概念;</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> 任务栈</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> 每个应用都有一个任务栈,是用来存放Activity的,功能类似于函数调用的栈,先后顺序代表了Activity的出现顺序;比如Activity1–>Activity2–>Activity3,则任务栈为:</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"></p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">二、启动模式</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">(1)standard:每次激活Activity时(startActivity),都创建Activity实例,并放入任务栈;</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"></p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">(2)singleTop:如果某个Activity自己激活自己,即任务栈栈顶就是该Activity,则不需要创建,其余情况都要创建Activity实例;</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"></p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">(3)singleTask:如果要激活的那个Activity在任务栈中存在该实例,则不需要创建,只需要把此Activity放入栈顶,并把该Activity以上的Activity实例都pop;</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"></p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">(4)singleInstance:如果应用1的任务栈中创建了MainActivity实例,如果应用2也要激活MainActivity,则不需要创建,两应用共享该Activity实例;</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"></p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> </p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> </p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">SingTask的应用:</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> 可以用来退出整个应用。</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> 将主Activity设为SingTask模式,然后在要退出的Activity中转到主Activity,然后重写主Activity的onNewIntent函数,并在函数中加上一句finish。</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> </p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> </p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">附:</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">退出单个Activity方法:</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> 调用finish</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> 杀死该进程:killprocess(Process.mId)</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> 终止正在运行的虚拟机:system.exit()</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> </p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">退出整个应用:</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> 制造抛异常导致整个程序退出</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> 将所有的activity放入到一个list中,然后在需要退出的时候,将所有的activity,finish掉</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> 通过广播来完成退出功能</p><p style="line-height: 11.1429px; margin-right: auto; margin-left: auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"> 通过广播来完成退出功能,具体实现过程是这样的:在每个Activity创建时(onCreate时)给Activity注册一个广播接收器,当退出时发送该广播即可。大概的代码如下:</p><div style="border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin: 5px 0px; font-family: 'Courier New' !important; font-size: 12px !important; background-color: rgb(245, 245, 245);"><div style="margin-top: 5px;"></div><div style="margin-top: 5px;"></div><pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;">@Override
</span>protected void onCreate(Bundle savedInstanceState) {
</span><span style="line-height: 1.5 !important;">super</span><span style="line-height: 1.5 !important;">.onCreate(savedInstanceState);
IntentFilter filter </span>= <span style="line-height: 1.5 !important;">new</span><span style="line-height: 1.5 !important;"> IntentFilter();
filter.addAction(</span>"finish"<span style="line-height: 1.5 !important;">);
registerReceiver(mFinishReceiver, filter);
……
}
</span>private BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {
@Override
</span><span style="line-height: 1.5 !important;">public</span> <span style="line-height: 1.5 !important;">void</span><span style="line-height: 1.5 !important;"> onReceive(Context context, Intent intent) {
</span><span style="line-height: 1.5 !important;">if</span>("finish"<span style="line-height: 1.5 !important;">.equals(intent.getAction())) {
Log.e(</span>"#########", "I am " +<span style="line-height: 1.5 !important;"> getLocalClassName()
</span>+ ",now finishing myself..."<span style="line-height: 1.5 !important;">);
finish();
}
}
};</span></pre><div style="margin-top: 5px;"></div><div style="margin-top: 5px;"></div></div><div> 把上面的代码写在一个基类里面,这样,在需要退出的地方调用close,发送广播,每个Activity接收到广播后,就会将自己finish掉。 </div>