Android中的Activity四种启动模式

1:standard 标准模式,不需要配置,系统默认的加载模式
启动目标Activity时,Android总会为目标Activity创建一个新的实例,并放到当前Task栈中,这种模式不会启动新的Task,新的Activity将被添加到原有的Task中
例子代码如下:

 1     package com.example.androidgov;
 2 
 3     import android.app.Activity;
 4     import android.content.Intent;
 5     import android.os.Bundle;
 6     import android.view.View;
 7     import android.widget.TextView;
 8 
 9     public class StandardActivity extends Activity {
10 
11         TextView textview1 = null;
12         
13         @Override
14         protected void onCreate(Bundle savedInstanceState) {
15             super.onCreate(savedInstanceState);
16             setContentView(R.layout.activity_standard);
17             textview1 = (TextView)findViewById(R.id.stand_textview1);
18             textview1.setText(this.getTaskId() + "--" + this.toString());
19         }
20 
21         public void startActivity( View view ) {
22             Intent intent = new Intent(StandardActivity.this, StandardActivity.class);
23             startActivity(intent);
24         }
25     }
StandardActivity

布局文件如下:

 1     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2         xmlns:tools="http://schemas.android.com/tools"
 3         android:id="@+id/container"
 4         android:layout_width="match_parent"
 5         android:layout_height="match_parent"
 6         android:orientation="vertical"
 7         tools:context="com.example.androidgov.StandardActivity"
 8         tools:ignore="MergeRootFrame" >
 9 
10         <Button
11             android:layout_width="match_parent"
12             android:layout_height="wrap_content"
13             android:onClick="startActivity"
14             android:text="startMySelf" />
15 
16         <TextView
17             android:id="@+id/stand_textview1"
18             android:layout_width="match_parent"
19             android:layout_height="wrap_content" />
20 
21     </LinearLayout>
activity_standard

运行结果如下图:

点击启动startMyself按钮

再次点击

由此可见,每次都在同一个Task中启动新的Activity

2:singleTop Task顶单例模式
与standard模式差不多,只是如果要被启动的Activity已经位于Task栈顶,那么这个Activity将不会被启动,不会生成Activity实例。而如果要被启动的Activity没有位于栈顶,系统会创建Activity实例,并把它置于Task栈顶
例子代码(AndroidManifest.xml中,activity节点配置一个launchMode属性,如下所示)

 1     <activity
 2                 android:name="com.example.androidgov.SingleTopActivity"
 3                 android:label="@string/title_activity_single_top"
 4                 android:launchMode="singleTop" >
 5                 <intent-filter>
 6                     <action android:name="android.intent.action.MAIN" />
 7 
 8                     <category android:name="android.intent.category.LAUNCHER" />
 9                 </intent-filter>
10             </activity>
11             <activity
12                 android:name="com.example.androidgov.OtherActivity"
13                 android:label="@string/title_activity_other" >
14             </activity>
AndroidManifest

SingleTopActivity.java代码

 1     package com.example.androidgov;
 2 
 3     import android.app.Activity;
 4     import android.content.Intent;
 5     import android.os.Bundle;
 6     import android.view.View;
 7     import android.widget.TextView;
 8 
 9     public class SingleTopActivity extends Activity {
10 
11         TextView textview1 = null;
12         
13         @Override
14         protected void onCreate(Bundle savedInstanceState) {
15             super.onCreate(savedInstanceState);
16             setContentView(R.layout.activity_single_top);
17             textview1 = (TextView)findViewById(R.id.singleTop_textview1);
18             textview1.setText(this.getTaskId() + "--" + this.toString());
19         }
20 
21         
22         public void startSecond( View view ) {
23             Intent intent = new Intent(SingleTopActivity.this, OtherActivity.class);
24             startActivity(intent);
25         }
26         
27         public void startSelf( View view ) {
28             Intent intent = new Intent(SingleTopActivity.this, SingleTopActivity.class);
29             startActivity(intent);
30         }
31     }
SingleTopActivity

布局文件:

 1     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2         xmlns:tools="http://schemas.android.com/tools"
 3         android:id="@+id/container"
 4         android:layout_width="match_parent"
 5         android:layout_height="match_parent"
 6         android:orientation="vertical"
 7         tools:context="com.example.androidgov.SingleTopActivity"
 8         tools:ignore="MergeRootFrame" >
 9 
10         <Button
11             android:layout_width="match_parent"
12             android:layout_height="wrap_content"
13             android:onClick="startSecond"
14             android:text="startOther" />
15 
16 
17         <Button
18             android:layout_width="match_parent"
19             android:layout_height="wrap_content"
20             android:onClick="startSelf"
21             android:text="startMyself" />
22 
23         <TextView
24             android:id="@+id/singleTop_textview1"
25             android:layout_width="match_parent"
26             android:layout_height="wrap_content" />
27 
28     </LinearLayout>
activity_single_top

OtherActivity.java代码文件

 1     package com.example.androidgov;
 2 
 3     import android.app.Activity;
 4     import android.content.Intent;
 5     import android.os.Bundle;
 6     import android.view.View;
 7     import android.widget.TextView;
 8 
 9     public class OtherActivity extends Activity {
10 
11         TextView textview1 = null;
12         
13         @Override
14         protected void onCreate(Bundle savedInstanceState) {
15             super.onCreate(savedInstanceState);
16             setContentView(R.layout.activity_other);
17             textview1 = (TextView)findViewById(R.id.other_textview1);
18             textview1.setText(this.getTaskId() + "--" + this.toString());
19         }
20 
21         public void startActivity( View view ) {
22             Intent intent = new Intent(OtherActivity.this, SingleTopActivity.class);
23             startActivity(intent);
24         }
25     }
OtherActivity

布局文件:

 1     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2         xmlns:tools="http://schemas.android.com/tools"
 3         android:id="@+id/container"
 4         android:layout_width="match_parent"
 5         android:layout_height="match_parent"
 6         android:orientation="vertical"
 7         tools:context="com.example.androidgov.StandardActivity"
 8         tools:ignore="MergeRootFrame" >
 9 
10         <Button
11             android:layout_width="match_parent"
12             android:layout_height="wrap_content"
13             android:onClick="startActivity"
14             android:text="startSingleTop" />
15 
16 
17         <TextView
18             android:id="@+id/other_textview1"
19             android:layout_width="match_parent"
20             android:layout_height="wrap_content" />
21 
22     </LinearLayout>
activity_other

运行结果如图(运行过程为:开启->点击startOther->点击startSingleTop->点击startSelf->点击startOther)

由以上图可知,如果要启动Activity不在栈顶,则会生成实例,置于栈顶位置,如果要启动Activity已经在栈顶,则不会生成实例

3:singleTask Task内单例模式
如果要启动的Activity没有,则系统生成Activity实例,并把它置于Task栈顶
如果要启动的Activity已经存在,但没有位于栈顶,系统把它之上的其他Activity全部移出栈,使得该Activity置于栈顶
如果要启动的Activity已经存在,而且已经位于栈顶,则系统不会生成相应实例
示例代码可以复用SingleTop中的代码进行测试,这里不再演示
4:singleInstance 全局单例模式
如果要启动的Activity不存在,系统会创建一个新的Task,再创建一个Activity实例,将这个实例加入这个新的Task中
如果要启动的Activity已经存在,无论它位于那个应用程序中,系统会把该Activity所在的Task转到前台,从而使该Activity显示出来
另外,这种模式启动的Activity存在的Task中只有这一个Activity,位于栈顶
示例代码可以复用SingleTop中的代码,只是修改AndroidManifest.xml中的配置

 1     <activity
 2                 android:name="com.example.androidgov.SingleTopActivity"
 3                 android:label="@string/title_activity_single_top" >
 4                 <intent-filter>
 5                     <action android:name="android.intent.action.MAIN" />
 6 
 7                     <category android:name="android.intent.category.LAUNCHER" />
 8                 </intent-filter>
 9             </activity>
10             <activity
11                 android:name="com.example.androidgov.OtherActivity"
12                 android:label="@string/title_activity_other"
13                 android:launchMode="singleInstance" >
14             </activity>
AndroidManifest

运行如图(运行过程是:启动->点击startSelf->点击startSelf->点击startOther->点击startSingleTop->点击startSelf->点击startOther)

由上图可知,如果以SingleInstance启动Activity,它会生成一个新的示例和一个Task,该实例位于该Task中。如果这个Activity已经存在,启动它的时候只是把它置于前台显示出来,并不会再生成新实例。而且,这个新的Task中只有这一个实例,从这个实例启动的其他Activity不会置于这个新的Task中

原文地址:https://www.cnblogs.com/creasylai19/p/3771557.html