首页 > 代码库 > activity启动模式之singleInstance

activity启动模式之singleInstance

activity启动模式之singleInstance

一、简介

技术分享

Log.d("C2", getTaskId()+"");里面的getTaskId()可以获取activity栈的编号

 

二、代码实例

结果图:

技术分享

技术分享

 

代码:

activity.C1

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

activity.C2

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

activity.C3

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

/activityLaunchSingleInstance/AndroidManifest.xml

 1 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 2     package="com.example.activityLaunchSingleInstance"
 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="singleInstance">
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>

 

activity启动模式之singleInstance