activity启动模式之singleTask

时间:2022-06-26 20:42:02

activity启动模式之singleTask

一、简介

activity启动模式之singleTask

如果另外一个应用调用了C2,C2在栈底,如果这个程序里面再嗲用C1,C3,C2,那么这个C2就是调用onNewIntant的,C1和C3都被销毁了;

另一个程序>c2>c1>c3>c2,实际只剩:另一个程序>c2。

 

二、代码实例

效果图:

activity启动模式之singleTask

activity启动模式之singleTask

上面就是调用了onNewIntent方法的情况

 

代码:

activity.C1

 1 package activity;
2
3
4
5
6 import com.example.activityLaunchSingleTask.R;
7
8 import android.app.Activity;
9 import android.content.Intent;
10 import android.os.Bundle;
11 import android.view.View;
12 import android.view.View.OnClickListener;
13 import android.widget.Button;
14
15
16
17 public class C1 extends Activity implements OnClickListener{
18 private Button btn_c1;//创建一个button对象
19 private Button btn_c2;//创建一个button对象
20 private Button btn_c3;//创建一个button对象
21 protected void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);//父类操作
23 setContentView(R.layout.activity_main);//引入名为activity_main的界面
24 setTitle("我是C1");
25 btn_c1=(Button) findViewById(R.id.btn_c1);//找id为btn_openActivity的button
26 btn_c2=(Button) findViewById(R.id.btn_c2);//找id为btn_openActivity的button
27 btn_c3=(Button) findViewById(R.id.btn_c3);//找id为btn_openActivity的button
28 btn_c1.setOnClickListener(this);
29 btn_c2.setOnClickListener(this);
30 btn_c3.setOnClickListener(this);
31 }
32 @Override
33 public void onClick(View v) {
34 // TODO Auto-generated method stub
35 switch (v.getId()) {
36 case R.id.btn_c1:
37 Intent intent=new Intent();
38 intent.setClass(C1.this, C1.class);
39 startActivity(intent);
40 break;
41 case R.id.btn_c2:
42 Intent intent2=new Intent();
43 intent2.setClass(C1.this, C2.class);
44 startActivity(intent2);
45 break;
46 case R.id.btn_c3:
47 Intent intent3=new Intent();
48 intent3.setClass(C1.this, C3.class);
49 startActivity(intent3);
50 break;
51 default:
52 break;
53 }
54 }
55 }

activity.C2

 1 package activity;
2
3
4
5
6 import com.example.activityLaunchSingleTask.R;
7
8 import android.app.Activity;
9 import android.content.Intent;
10 import android.os.Bundle;
11 import android.view.View;
12 import android.view.View.OnClickListener;
13 import android.widget.Button;
14 import android.widget.Toast;
15
16
17
18 public class C2 extends Activity implements OnClickListener{
19 private Button btn_c1;//创建一个button对象
20 private Button btn_c2;//创建一个button对象
21 private Button btn_c3;//创建一个button对象
22 protected void onCreate(Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState);//父类操作
24 setContentView(R.layout.activity_main);//引入名为activity_main的界面
25 setTitle("我是C2");
26 btn_c1=(Button) findViewById(R.id.btn_c1);//找id为btn_openActivity的button
27 btn_c2=(Button) findViewById(R.id.btn_c2);//找id为btn_openActivity的button
28 btn_c3=(Button) findViewById(R.id.btn_c3);//找id为btn_openActivity的button
29 btn_c1.setOnClickListener(this);
30 btn_c2.setOnClickListener(this);
31 btn_c3.setOnClickListener(this);
32 }
33 @Override
34 public void onClick(View v) {
35 // TODO Auto-generated method stub
36 switch (v.getId()) {
37 case R.id.btn_c1:
38 Intent intent=new Intent();
39 intent.setClass(C2.this, C1.class);
40 startActivity(intent);
41 break;
42 case R.id.btn_c2:
43 Intent intent2=new Intent();
44 intent2.setClass(C2.this, C2.class);
45 startActivity(intent2);
46 break;
47 case R.id.btn_c3:
48 Intent intent3=new Intent();
49 intent3.setClass(C2.this, C3.class);
50 startActivity(intent3);
51 break;
52 default:
53 break;
54 }
55 }
56
57 @Override
58 protected void onNewIntent(Intent intent) {
59 // TODO Auto-generated method stub
60 super.onNewIntent(intent);
61 Toast.makeText(this, "onNewIntent", Toast.LENGTH_SHORT).show();
62 }
63 }

activity.C3

 1 package activity;
2
3
4
5
6 import com.example.activityLaunchSingleTask.R;
7
8 import android.app.Activity;
9 import android.content.Intent;
10 import android.os.Bundle;
11 import android.view.View;
12 import android.view.View.OnClickListener;
13 import android.widget.Button;
14
15
16
17 public class C3 extends Activity implements OnClickListener{
18 private Button btn_c1;//创建一个button对象
19 private Button btn_c2;//创建一个button对象
20 private Button btn_c3;//创建一个button对象
21 protected void onCreate(Bundle savedInstanceState) {
22 super.onCreate(savedInstanceState);//父类操作
23 setContentView(R.layout.activity_main);//引入名为activity_main的界面
24 setTitle("我是C3");
25 btn_c1=(Button) findViewById(R.id.btn_c1);//找id为btn_openActivity的button
26 btn_c2=(Button) findViewById(R.id.btn_c2);//找id为btn_openActivity的button
27 btn_c3=(Button) findViewById(R.id.btn_c3);//找id为btn_openActivity的button
28 btn_c1.setOnClickListener(this);
29 btn_c2.setOnClickListener(this);
30 btn_c3.setOnClickListener(this);
31 }
32 @Override
33 public void onClick(View v) {
34 // TODO Auto-generated method stub
35 switch (v.getId()) {
36 case R.id.btn_c1:
37 Intent intent=new Intent();
38 intent.setClass(C3.this, C1.class);
39 startActivity(intent);
40 break;
41 case R.id.btn_c2:
42 Intent intent2=new Intent();
43 intent2.setClass(C3.this, C2.class);
44 startActivity(intent2);
45 break;
46 case R.id.btn_c3:
47 Intent intent3=new Intent();
48 intent3.setClass(C3.this, C3.class);
49 startActivity(intent3);
50 break;
51 default:
52 break;
53 }
54 }
55 }

/activityLaunchSingleTask/AndroidManifest.xml

 1 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2 package="com.example.activityLaunchSingleTask"
3 android:versionCode="1"
4 android:versionName="1.0" >
5
6 <uses-sdk
7 android:minSdkVersion="8"
8 android:targetSdkVersion="19" />
9
10 <application
11 android:allowBackup="true"
12 android:icon="@drawable/ic_launcher"
13 android:label="@string/app_name"
14 android:theme="@style/AppTheme" >
15 <activity
16 android:name="activity.C1"
17 android:label="@string/app_name" >
18 <intent-filter>
19 <action android:name="android.intent.action.MAIN" />
20
21 <category android:name="android.intent.category.LAUNCHER" />
22 </intent-filter>
23 </activity>
24 <activity android:name="activity.C2" android:launchMode="singleTask">
25 <intent-filter>
26 <action android:name="com.example.activityLaunchSingleTask" />
27 <category android:name="android.intent.category.DEFAULT" />
28 </intent-filter>
29 </activity>

30 <activity android:name="activity.C3"></activity>
31 </application>
32
33 </manifest>

/activityLaunchSingleTask/res/layout/activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <Button
8 android:id="@+id/btn_c1"
9 android:layout_width="match_parent"
10 android:layout_height="50dp"
11 android:layout_weight="0.00"
12 android:text="@string/btn_c1" />
13
14 <Button
15 android:id="@+id/btn_c2"
16 android:layout_width="match_parent"
17 android:layout_height="wrap_content"
18 android:text="@string/btn_c2" />
19
20 <Button
21 android:id="@+id/btn_c3"
22 android:layout_width="match_parent"
23 android:layout_height="wrap_content"
24 android:text="@string/btn_c3" />
25
26 </LinearLayout>