activity启动模式之singleInstance
singleInstance 这个启动模式,是这个activity占有唯一的一个栈。
看如下demo
manifest.xml
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:logo="@drawable/action_mode_search_normal_white_p" android:theme="@style/AppTheme" > <activity android:name=".HeadSetActivity" android:label="@string/app_name" android:launchMode="singleTop" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MyActivity1" android:label="@string/app_name" android:launchMode="singleInstance" > <intent-filter> <action android:name="activity_one" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".MyActivity2" android:label="@string/app_name" android:launchMode="standard" > <intent-filter> <action android:name="activity_two" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application>
三个activity代码
HeadSetActivity.java
package com.example.alert; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class HeadSetActivity extends Activity { private TextView textView; private Button button1; private Button button2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.tv); button1 = (Button) findViewById(R.id.bt1); button2 = (Button) findViewById(R.id.bt2); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub //启动activity1 Intent intent = new Intent("activity_one"); startActivity(intent); } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub //启动activity2 Intent intent = new Intent("activity_two"); startActivity(intent); } }); } //这个方法主要是接受子activity返回的数据的 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); //requestCode 这个是启动子activity的时候,父activity给予的标签,父分组管理 //resultCode 这个是子activity返回来的标签,是子activity分组管理 //data里面是子activity返回来的数据 if (requestCode == 1) { if (resultCode == 10) { String string = data.getExtras().getString("activity_finish"); textView.setText("com from activity 1 and finish"+" \n" + string); } else if (resultCode == 100) { String string = data.getExtras().getString("activity_no_finish"); textView.setText("com from activity 1 and no finish"+" \n" + string); } } else if (requestCode == 2) { if (resultCode == 100) { textView.setText("com from activity 2 and finish"); } else if (resultCode == 120) { textView.setText("com from activity 2 and no finish"); } } } }
MyActivity1.java
package com.example.alert; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MyActivity1 extends Activity { private TextView textView; private Button button1; private Button button2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.tv); textView.setText("i am activity1"); button1 = (Button) findViewById(R.id.bt1); button1.setText("HeadSetActivity"); button2 = (Button) findViewById(R.id.bt2); button2.setText("Activity2"); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(MyActivity1.this, HeadSetActivity.class); startActivity(intent); } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent("activity_two"); startActivity(intent); } }); } }
MyActivity2.java
package com.example.alert; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MyActivity2 extends Activity { private TextView textView; private Button button1; private Button button2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.tv); textView.setText("i am activity2"); button1 = (Button) findViewById(R.id.bt1); button1.setText("HeadSetActivity"); button2 = (Button) findViewById(R.id.bt2); button2.setText("Activity1"); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(MyActivity2.this, HeadSetActivity.class); startActivity(intent); } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent("activity_one"); startActivity(intent); } }); } }
下面我们按照如下的启动操作进行验证
HeadSetActivity->MyActivity1->MyActivity1->HeadSetActivity->MyActivity1
结果如下
Running activities (most recent first): TaskRecord{4305f1b8 #34 A=com.example.alert U=0 sz=1} Run #3: ActivityRecord{4305eef8 u0 com.example.alert/.MyActivity1 t34} TaskRecord{430b5f38 #33 A=com.example.alert U=0 sz=3} Run #2: ActivityRecord{426026a0 u0 com.example.alert/.HeadSetActivity t33} Run #1: ActivityRecord{4261ed58 u0 com.example.alert/.MyActivity2 t33} Run #0: ActivityRecord{42546340 u0 com.example.alert/.HeadSetActivity t33}
可以发现,这个task里面就只有始终只有一个MyActivity1,而且,它启动的activity,也不会在这个task里面,再者,它不许要像singleTask那样,需要
taskAffinity 进行设定