android Task Activity launchMode="singleTask" singleInstance 程式碼分析
<?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 後面設定無效
相關文章
- 【筆記】【Android】Activity的Task模式筆記Android模式
- 喜聞樂見之Activity的launchMode
- Android Activity啟動流程原始碼分析Android原始碼
- Android原始碼分析:Activity啟動流程Android原始碼
- Android 8.0 原始碼分析 (四) Activity 啟動Android原始碼
- Flutter Android 端 Activity/Fragment 流程原始碼分析FlutterAndroidFragment原始碼
- Android系統原始碼分析--Activity啟動過程Android原始碼
- Activity原始碼分析原始碼
- Android Activity Deeplink啟動來源獲取原始碼分析Android原始碼
- Intent.FLAG_ACTIVITY_NEW_TASKIntent
- Android UsbDeviceManager 程式碼分析Androiddev
- Android程式啟動與Activity顯示Android
- Activity啟動流程原始碼分析原始碼
- Android進階(九)Activity外掛化和VirtualApk分析AndroidAPK
- Android HAL 層框架分析以及程式碼示例Android框架
- [Android]關閉所有Activity,開啟某個ActivityAndroid
- Android Activity生命週期Android
- Android之Activity全面解析Android
- 仿寫Android的ActivityAndroid
- Android Activity的基本理解Android
- Android Activity那點事Android
- Android中Context、Activity、ApplicatioAndroidContextAPP
- OpenMP task construct 實現原理以及原始碼分析Struct原始碼
- onClick事件中點選跳轉新的activity提示FLAG_ACTIVITY_NEW_TASK的奇怪bug事件
- Android 8.0 原始碼分析 (一) SystemServer 程式啟動Android原始碼Server
- Android基礎-Activity基本使用Android
- Android四大元件——Activity——Activity的生命週期Android元件
- Android 原始碼分析之 AsyncTask 原始碼分析Android原始碼
- Activity啟動流程分析
- Android Adobe Reader 任意程式碼執行分析(附POC)Android
- Android觸控事件全過程分析:由產生到Activity.dispatchTouchEvent()Android事件
- Android之Activity基類封裝Android封裝
- Android基礎之Activity全解析Android
- Android-Activity的啟動模式Android模式
- Android Choreographer 原始碼分析Android原始碼
- activity影象亂碼
- activity影像亂碼
- App 竟然是這樣跑起來的 —— Android App/Activity 啟動流程分析APPAndroid
- 深入理解Android 之 Activity啟動流程(Android 10)Android