android Task Activity launchMode="singleTask" singleInstance 程式碼分析

心鑫發表於2013-12-16

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testsinglainstance.MainActivity"
            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=".Activity2"
            android:launchMode="singleInstance"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name=".Activity3"
            android:launchMode="singleTask"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

package com.example.testsinglainstance;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;


public class MainActivity extends Activity {


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		TextView tv = (TextView) findViewById(R.id.tv);
		tv.setText("1");
		System.out.println("1-oncreate->TaskId:"+this.getTaskId()+",hashcode:"+this.hashCode());
		tv.setOnClickListener(new OnClickListener() {


			@Override
			public void onClick(View v) {
				MainActivity.this.startActivity(new Intent(MainActivity.this,
						Activity2.class));


			}
		});
	}
	@Override
	protected void onDestroy() {
		System.out.println("1-onDestroy");
		super.onDestroy();
	}
}


package com.example.testsinglainstance;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;


public class Activity2 extends Activity {


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		TextView tv=(TextView) findViewById(R.id.tv);
		tv.setText("2");
		System.out.println("2-oncreate->TaskId:"+this.getTaskId()+",hashcode:"+this.hashCode());
		tv.setOnClickListener(new OnClickListener() {


			@Override
			public void onClick(View v) {
				Activity2.this.startActivity(new Intent(Activity2.this,
						Activity3.class));


			}
		});
	}
	@Override
	protected void onDestroy() {
		System.out.println("2-onDestroy");
		super.onDestroy();
	}


}
package com.example.testsinglainstance;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;


public class Activity3 extends Activity {


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		TextView tv=(TextView) findViewById(R.id.tv);
		tv.setText("3");
		System.out.println("3-oncreate->TaskId:"+this.getTaskId()+",hashcode:"+this.hashCode());
		tv.setOnClickListener(new OnClickListener() {


			@Override
			public void onClick(View v) {
				Activity3.this.startActivity(new Intent(Activity3.this,
						MainActivity.class));


			}
		});
	}
@Override
protected void onDestroy() {
	System.out.println("3-onDestroy");
	super.onDestroy();
}


}



點選幾次按紐後log如下:
12-17 08:48:45.632: I/System.out(5499): 1-oncreate->TaskId:27,hashcode:1079088424
12-17 08:49:03.539: I/System.out(5499): 2-oncreate->TaskId:28,hashcode:1079128160
12-17 08:49:04.273: I/System.out(5499): 3-oncreate->TaskId:27,hashcode:1079154544
12-17 08:49:04.843: I/System.out(5499): 1-oncreate->TaskId:27,hashcode:1079178912
12-17 08:49:05.953: I/System.out(5499): 1-onDestroy
12-17 08:49:06.242: I/System.out(5499): 1-oncreate->TaskId:27,hashcode:1079210512
12-17 08:49:07.289: I/System.out(5499): 1-onDestroy
12-17 08:49:07.523: I/System.out(5499): 1-oncreate->TaskId:27,hashcode:1079241920
12-17 08:49:07.953: I/System.out(5499): 1-onDestroy
12-17 08:49:08.179: I/System.out(5499): 1-oncreate->TaskId:27,hashcode:1079273184
12-17 08:49:08.531: I/System.out(5499): 1-onDestroy
12-17 08:49:08.742: I/System.out(5499): 1-oncreate->TaskId:27,hashcode:1079304448

在3介面按返回鍵

12-17 08:53:26.968: I/System.out(5499): 3-onDestroy
12-17 08:53:27.656: I/System.out(5499): 1-onDestroy
12-17 08:53:28.687: I/System.out(5499): 2-onDestroy


在2介面按返回鍵 

12-17 08:52:04.882: I/System.out(5499): 2-onDestroy
12-17 08:52:05.937: I/System.out(5499): 1-onDestroy
12-17 08:52:09.601: I/System.out(5499): 3-onDestroy
12-17 08:52:11.562: I/System.out(5499): 1-onDestroy
結論1

:singleInstance 不新建actvity例項 ,例項在新的Task內

singleTask 不新建actvity例項 ,例項在原Task內

預設每次呼叫都生成一個新的例項

返回的時候,先返回到當前Task的下個Activity,當前Task到底,再返回到其他TasK

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testsinglainstance.MainActivity"
            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=".Activity2"
            android:launchMode="singleTask"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name=".Activity3"
            android:launchMode="singleTask"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

按文字框log如下:

12-17 08:59:01.054: I/System.out(5598): 1-oncreate->TaskId:31,hashcode:1079096120
12-17 08:59:08.070: I/System.out(5598): 2-oncreate->TaskId:31,hashcode:1079136432
12-17 08:59:08.375: I/System.out(5598): 3-oncreate->TaskId:31,hashcode:1079162784
12-17 08:59:09.406: I/System.out(5598): 1-oncreate->TaskId:31,hashcode:1079186984
12-17 08:59:09.867: I/System.out(5598): 3-onDestroy
12-17 08:59:09.960: I/System.out(5598): 1-onDestroy
12-17 08:59:10.320: I/System.out(5598): 3-oncreate->TaskId:31,hashcode:1079213816
12-17 08:59:10.765: I/System.out(5598): 1-oncreate->TaskId:31,hashcode:1079236960
12-17 08:59:11.171: I/System.out(5598): 3-onDestroy
12-17 08:59:11.257: I/System.out(5598): 1-onDestroy
12-17 08:59:11.585: I/System.out(5598): 3-oncreate->TaskId:31,hashcode:1079263712
12-17 08:59:12.070: I/System.out(5598): 1-oncreate->TaskId:31,hashcode:1079286912

停留在3介面返回

12-17 09:01:26.585: I/System.out(5667): 3-onDestroy
12-17 09:01:27.351: I/System.out(5667): 2-onDestroy
12-17 09:01:28.421: I/System.out(5667): 1-onDestroy

停留在2介面返回

12-17 09:02:09.914: I/System.out(5667): 2-onDestroy
12-17 09:02:10.601: I/System.out(5667): 1-onDestroy


結論2:

singleTask 在原Tast內

1個Task內只保留一個singleTask 後面設定無效




相關文章