1、包含两个Application,Application1包含2个Activity,Application2包含3个Activity,Application1的第二个Activity为singleTask,

 

Application1中,Application名ActivityModeTest1,包名:com.activitymode.test1

ActivityModeTest1Activity和ActivityModeTest2;

main.xml,main2.xml.strings.xml.Androidmanifest.xml

ActivityModeTest1Activity:(红色是主要有区别的部分)

package com.activitymode.test1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
public class ActivityModeTest1Activity extends Activity {
private Button btn1;
private static String TAG="ActivityModeTest1Activity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(listener1);
int id=this.getTaskId();
Log.v(TAG, "11111onCreate"+",id=="+id);
}
OnClickListener listener1= new OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent t = new Intent();
t.setClass(ActivityModeTest1Activity.this, ActivityModeTest2.class);
// t.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//实验2:把AndroidManifest.xml中的ActivityModeTest2的singleTask去掉,换成这个。
startActivity(t);
}

};
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.v(TAG, "11111onDestroy");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.v(TAG, "1111onPause");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//Log.v(TAG, "1111onResume");
Log.v(TAG, "111onResume"+",id=="+this.getTaskId());
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.v(TAG, "1111onStart");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.v(TAG, "1111onStop");
}

}

 

ActivityModeTest2:

package com.activitymode.test1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
public class ActivityModeTest2 extends Activity {
private Button btn2;
private static String TAG="ActivityModeTest1Activity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
btn2 = (Button)findViewById(R.id.btn2);
btn2.setOnClickListener(listener1);
Log.v(TAG, "2222onCreate"+",id=="+this.getTaskId());
}
OnClickListener listener1= new OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent t = new Intent();
t.setAction("com.ac.mode1");
 //t.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//实验2:把AndroidManifest.xml中的ActivityModeTest2的singleTask去掉,换成这个。
            startActivity(t);
}

};
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.v(TAG, "2222onDestroy");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.v(TAG, "2222onPause");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// Log.v(TAG, "222onResume");
Log.v(TAG, "222onResume"+",id=="+this.getTaskId());
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.v(TAG, "2222onStart");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.v(TAG, "2222onStop");
}

}

 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn1" />

</LinearLayout>

main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn2" />
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.activitymode.test1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ActivityModeTest1Activity"
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=".ActivityModeTest2"
android:launchMode="singleTask">

<intent-filter>
<action android:name="com.ac.mode2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

</manifest>




Application2:名为ActivityModeTest2,包名:com.acmode.test2

包括三个Activity,main.xml,main2.xml.main3.xml.AnroidManifest.xml

 

ActivityModeTes3Activity

package com.acmode.test2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ActivityModeTes3Activity extends Activity {
/** Called when the activity is first created. */
private Button btn1;
private static String TAG="ActivityModeTest1Activity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(listener1);
Log.v(TAG, "3333onCreate");
}
OnClickListener listener1= new OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent t = new Intent();
t.setClass(ActivityModeTes3Activity.this, ActivityModeTes4Activity.class);
startActivity(t);
}

};
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.v(TAG, "3333onDestroy");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.v(TAG, "3333onPause");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//Log.v(TAG, "1111onResume");
Log.v(TAG, "3333onResume"+",id=="+this.getTaskId());
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.v(TAG, "3333onStart");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.v(TAG, "3333onStop");
}


}

 

 

ActivityModeTes4Activity:

package com.acmode.test2;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ActivityModeTes4Activity extends Activity {
/** Called when the activity is first created. */
private Button btn1;
private static String TAG="ActivityModeTest1Activity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
btn1 = (Button)findViewById(R.id.btn2);
btn1.setOnClickListener(listener1);
Log.v(TAG, "444onCreate");
}
OnClickListener listener1= new OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent t = new Intent();
t.setClass(ActivityModeTes4Activity.this, ActivityModeTes5Activity.class);
//t.setAction("com.ac.mode2");
startActivity(t);
}

};
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.v(TAG, "444onDestroy");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.v(TAG, "4444onPause");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//Log.v(TAG, "1111onResume");
Log.v(TAG, "4444onResume"+",id=="+this.getTaskId());
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.v(TAG, "4444onStart");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.v(TAG, "444onStop");
}


}

 

 

 

ActivityModeTes5Activity:

package com.acmode.test2;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ActivityModeTes5Activity extends Activity {
/** Called when the activity is first created. */
private Button btn1;
private static String TAG="ActivityModeTest1Activity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
btn1 = (Button)findViewById(R.id.btn3);
btn1.setOnClickListener(listener1);
Log.v(TAG, "555onCreate");
}
OnClickListener listener1= new OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent t = new Intent();
//t.setClass(ActivityModeTes4Activity.this, ActivityModeTest2.class);
t.setAction("com.ac.mode2");
startActivity(t);
}

};
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.v(TAG, "55555onDestroy");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.v(TAG, "5555onPause");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//Log.v(TAG, "1111onResume");
Log.v(TAG, "5555onResume"+",id=="+this.getTaskId());
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.v(TAG, "5555onStart");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.v(TAG, "5555onStop");
}


}

 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn1" />
</LinearLayout>

 

main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn2" />
</LinearLayout>

main3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/btn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn3" />
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.acmode.test2"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ActivityModeTes3Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="com.ac.mode1" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".ActivityModeTes4Activity" >
</activity>
<activity android:name=".ActivityModeTes5Activity" >
</activity>
</application>

</manifest>




跳转图见自己本上圈1;

示例2:

如果把Application1中的ActivityModeTest2在AndroidManifest.xml中的SingleTask去掉,在ActivityModeTest1Activity中加上setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);

跳转图见自己本上圈2;由于两者在一个Application中,所以,setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);并不会重启一个新Task,还是在老Task里面继续。

 

示例3:

如果把Application1中的ActivityModeTest2在AndroidManifest.xml中的SingleTask去掉,在ActivityModeTest2中加上setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);即不同Application之间的activity启动,用新建Task方式,(Ac2-->Ac3新建Task)

跳转图见自己本上圈3;

 

总结:以上三个示例说明singleTask与setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)区别。

(1)对于singleTask:

当从其他Activity F跳到已经存在实例的singleTask的Activity G的时候,看G的属性为singleTask,把G上面的Activity都destroy掉(从离G最近的Activity开始往上挨个Destroy,直到启动G的那个Activity执行Destroy了为止),这时候跳到G(注意:整个过程不会new Task),此时back,跳到F,再back,回到home界面。
(2)对于setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
 a、如果后者Activity没有同样affinity的新Task,则创建;见示例3:

从ActivityMode5Activity跳转到ActivityMode2的时候,看ActivityMode2的android的属性,没有设置android:lauchmode,故ActivityMode2是默认standard模式,会新建一个ActivityMode2的实例instance,(注意:不会再new task!)

b、如果后者Activity有同样affinity的Task 2(即使不设置android:taskAffinity也有默认的),到这个有affinity的Task 2中。如示例2中,有 同样affinity的Task(即现在ActivityModeTest1Activity所在的Task,所以不会new一个新Task,还在原来Task中继续。)

 

 

 

下面讲singleTask或setFlags(Intent.ACTIVITY_NEW_TASK)之一与taskAffinity一起使用的情况:

 

 

 

posted on 2012-02-17 09:15  snowdrop  阅读(338)  评论(0编辑  收藏  举报