standard
singleTop
singleTask
singleInstance
Firtst, please refer to http://developer.android.com/guide/topics/manifest/activity-element.html#lmode for the attribute of activity.
In order to test, I need a project with 3 activities.
A1
A2
A3
The test way is let one activity call the other activity with an intent, and back to the previous activity with the Back Key. I only modified the launchMode attribute of A2.
public class A1 extends Activity {
/** Called when the activity is first created. */
private String mString;
private TextView txt1_history;
private OnClickListener mListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView txt1 = (TextView) findViewById(R.id.TextView1_intent);
txt1.setText(txt1.getText() + getIntent().getStringExtra("intent"));
txt1_history = (TextView) findViewById(R.id.TextView1_history);
Button button1_2 = (Button) findViewById(R.id.Button1_2);
Button button1_3 = (Button) findViewById(R.id.Button1_3);
mListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
if (v.getId() == R.id.Button1_2) {
intent.setClass(A1.this, A2.class);
} else if (v.getId() == R.id.Button1_3) {
intent.setClass(A1.this, A3.class);
}
intent.putExtra("intent", "Intent1");
intent.putExtra("history", mString);
startActivity(intent);
}
};
button1_2.setOnClickListener(mListener);
button1_3.setOnClickListener(mListener);
}
@Override
public void onResume() {
super.onResume();
mString = getIntent().getStringExtra("history");
if (mString == null) {
mString = "";
}
txt1_history.setText(mString + this.toString() + " --> \n");
mString = (String) txt1_history.getText();
}
}
public class A2 extends Activity {
private String mString;
private TextView txt2_history;
private OnClickListener mListener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
TextView txt2 = (TextView) findViewById(R.id.TextView2_intent);
txt2.setText(txt2.getText() + getIntent().getStringExtra("intent"));
txt2_history = (TextView) findViewById(R.id.TextView2_history);
Button button2_1 = (Button) findViewById(R.id.Button2_1);
Button button2_2 = (Button) findViewById(R.id.Button2_2);
Button button2_3 = (Button) findViewById(R.id.Button2_3);
mListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
if (v.getId() == R.id.Button2_1) {
intent.setClass(A2.this, A1.class);
} else if (v.getId() == R.id.Button2_2) {
intent.setClass(A2.this, A2.class);
} else if (v.getId() == R.id.Button2_3) {
intent.setClass(A2.this, A3.class);
}
intent.putExtra("intent", "Intent2");
intent.putExtra("history", mString);
startActivity(intent);
}
};
button2_1.setOnClickListener(mListener);
button2_2.setOnClickListener(mListener);
button2_3.setOnClickListener(mListener);
}
@Override
public void onResume() {
super.onResume();
mString = getIntent().getStringExtra("history");
if (mString == null) {
mString = "";
}
txt2_history.setText(mString + this.toString() + " --> \n");
mString = (String) txt2_history.getText();
}
}
public class A3 extends Activity {
private String mString;
private TextView txt3_history;
private OnClickListener mListener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
TextView txt3 = (TextView) findViewById(R.id.TextView3_intent);
txt3.setText(txt3.getText() + getIntent().getStringExtra("intent"));
txt3_history = (TextView) findViewById(R.id.TextView3_history);
Button button3_1 = (Button) findViewById(R.id.Button3_1);
Button button3_2 = (Button) findViewById(R.id.Button3_2);
mListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
if (v.getId() == R.id.Button3_1) {
intent.setClass(A3.this, A1.class);
} else if (v.getId() == R.id.Button3_2) {
intent.setClass(A3.this, A2.class);
}
intent.putExtra("intent", "Intent3");
intent.putExtra("history", mString);
startActivity(intent);
}
};
button3_1.setOnClickListener(mListener);
button3_2.setOnClickListener(mListener);
}
@Override
public void onResume() {
super.onResume();
mString = getIntent().getStringExtra("history");
if (mString == null) {
mString = "";
}
txt3_history.setText(mString + this.toString() + " --> \n");
mString = (String) txt3_history.getText();
}
}
最後是AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testintent" android:versionCode="1" android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".A1" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".A2" android:label="@string/app_name" />
<activity android:name=".A3" android:label="@string/app_name" />
</application>
</manifest>
If you don't set any attribute value for A2 or set
<activity android:name=".A2" android:label="@string/app_name" android:launchMode="standard"/>
the A2 is in standard mode.
I ran the activity in sequence A1 -> A2 -> A2 -> A2 -> A3, the results are as follows:
Please note, A2 is called 3 times,and system produced 3 instances of A2. As you can see,
com.testintent.A2@43e4a208
com.testintent.A2@43e528d8
com.testintent.A2@43e5aac8
there are 3 independent hashcodes.
You can find out the activities disappeared in order when you tap Back Key since A3.
A3 -> A2 -> A2 -> A2 -> A1
The order of A2 disappearance is
com.testintent.A2@43e5aac8
com.testintent.A2@43e528d8
com.testintent.A2@43e4a208
In sum, as you call startActivity() system will produce an independent instance and it will be located in the top of the activity stack.
沒有留言:
張貼留言