Activity之掃視畫面(Overview Screen),activityoverview
掃視畫面
掃視畫面(也稱為最新動向螢幕、最近工作清單或最近使用的應用)是一個系統層級 UI,其中列出了最近訪問過的 Activity 和任務。 使用者可以瀏覽該列表並選擇要恢複的任務,也可以通過滑動清除任務將其從列表中移除。 對於 Android 5.0 版本(API 層級 21),包含不同文檔的同一 Activity 的多個執行個體可能會以任務的形式顯示在掃視畫面中。前邊是文檔中說的,比較虛,下面來一個gif你就懂啦。
可以看到左邊的任務檢視是兩個重疊在一塊的,這也就是掃視畫面。
通常,您應該允許系統定義任務和 Activity 在掃視畫面中的顯示方法,並且無需修改此行為。不過,應用可以確定 Activity 在掃視畫面中的顯示方式和時間。 您可以使用 ActivityManager.AppTask 類來管理工作,使用 Intent 類的 Activity 標誌來指定某 Activity 添加到掃視畫面或從中移除的時間。 此外,您也可以使用 <activity> 屬性在資訊清單檔中設定該行為。
將任務添加到掃視畫面
通過使用 Intent 類的標誌新增工作,您可以更好地控制某文檔在掃視畫面中開啟或重新開啟的時間和方式。 使用 <activity> 屬性時,您可以選擇始終在新任務中開啟文檔,或選擇對文檔重複使用現有任務。
使用 Intent 標誌新增工作
為 Activity 建立新文檔時,可調用 ActivityManager.AppTask 類的 startActivity() 方法,以向其傳遞啟動 Activity 的 Intent。 要插入邏輯分行符號以便系統將 Activity 視為新任務顯示在掃視畫面中,可在啟動 Activity 的 Intent 的 addFlags() 方法中傳遞 FLAG_ACTIVITY_NEW_DOCUMENT 標誌。
注:FLAG_ACTIVITY_NEW_DOCUMENT 標誌取代了 FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET 標誌,後者自 Android 5.0(API 層級 21)起已棄用。
如果在建立新文檔時設定 FLAG_ACTIVITY_MULTIPLE_TASK 標誌,則系統始終會以目標 Activity 作為根建立新任務。此設定允許同一文檔在多個任務中開啟。以下代碼示範了主 Activity 如何執行此操作:
public void createNewDocument(View view) { final Intent newDocumentIntent = newDocumentIntent(); if (useMultipleTasks) { newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); } startActivity(newDocumentIntent); } private Intent newDocumentIntent() { boolean useMultipleTasks = mCheckbox.isChecked(); final Intent newDocumentIntent = new Intent(this, NewDocumentActivity.class); newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); newDocumentIntent.putExtra(KEY_EXTRA_NEW_DOCUMENT_COUNTER, incrementAndGet()); return newDocumentIntent; } private static int incrementAndGet() { Log.d(TAG, "incrementAndGet(): " + mDocumentCounter); return mDocumentCounter++; }}
註:使用 FLAG_ACTIVITY_NEW_DOCUMENT 標誌啟動的 Activity 必須具有在資訊清單檔中設定的 android:launchMode="standard" 屬性值(預設)。
當主 Activity 啟動新 Activity 時,系統會搜遍現有任務,看看是否有任務的 Intent 與 Activity 的 Intent 組件名稱和 Intent 資料相匹配。 如果未找到任務或者 Intent 包含 FLAG_ACTIVITY_MULTIPLE_TASK 標誌,則會以該 Activity 作為其根建立新任務。如果找到的話,則會將該任務轉到前台並將新 Intent 傳遞給 onNewIntent()。新 Activity 將獲得 Intent 並在掃視畫面中建立新文檔,如下例所示:
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new_document); mDocumentCount = getIntent() .getIntExtra(DocumentCentricActivity.KEY_EXTRA_NEW_DOCUMENT_COUNTER, 0); mDocumentCounterTextView = (TextView) findViewById( R.id.hello_new_document_text_view); setDocumentCounterText(R.string.hello_new_document_counter);}@Overrideprotected void onNewIntent(Intent intent) { super.onNewIntent(intent); /* If FLAG_ACTIVITY_MULTIPLE_TASK has not been used, this activity is reused to create a new document. */ setDocumentCounterText(R.string.reusing_document_counter);}使用 Activity 屬性新增工作
此外,Activity 還可以在其資訊清單檔中指定始終通過使用 <activity> 屬性 android:documentLaunchMode 進入新任務。 此屬性有四個值,會在使用者使用該應用開啟文檔時產生以下效果:
-
"
intoExisting"
-
該 Activity 會對文檔重複使用現有任務。這與
不設定
FLAG_ACTIVITY_MULTIPLE_TASK 標誌、但設定
FLAG_ACTIVITY_NEW_DOCUMENT 標誌所產生的效果相同,如上文的使用 Intent 標誌新增工作中所述。
-
"
always"
-
該 Activity 為文檔建立新任務,即便文檔已開啟也是如此。使用此值與同時設定
FLAG_ACTIVITY_NEW_DOCUMENT 和
FLAG_ACTIVITY_MULTIPLE_TASK標誌所產生的效果相同。
-
"
none”"
-
該 Activity 不會為文檔建立新任務。掃視畫面將按其預設對待此 Activity:為應用顯示單個任務,該任務將從使用者上次調用的任意 Activity 開始繼續執行。
-
"
never"
-
該 Activity 不會為文檔建立新任務。設定此值會替代
FLAG_ACTIVITY_NEW_DOCUMENT 和
FLAG_ACTIVITY_MULTIPLE_TASK 標誌的行為(如果在 Intent 中設定了其中一個標誌),並且掃視畫面將為應用顯示單個任務,該任務將從使用者上次調用的任意 Activity 開始繼續執行。
註:對於除 none 和 never 以外的值,必須使用 launchMode="standard" 定義 Activity。如果未指定此屬性,則使用 documentLaunchMode="none"。
移除任務
預設情況下,在 Activity 結束後,文檔任務會從掃視畫面中自動移除。 您可以使用 ActivityManager.AppTask 類、Intent 標誌或 <activity> 屬性替代此行為。
通過將 <activity> 屬性 android:excludeFromRecents 設定為 true,您可以始終將任務從掃視畫面中完全排除。
您可以通過將 <activity> 屬性 android:maxRecents 設定為整型值,設定應用能夠包括在掃視畫面中的最大任務數。預設值為 16。達到最大任務數後,最近最少使用的任務將從掃視畫面中移除。 android:maxRecents 的最大值為 50(記憶體不足的裝置上為 25);小於 1 的值無效。
使用 AppTask 類移除任務
在於掃視畫面建立新任務的 Activity 中,您可以通過調用 finishAndRemoveTask() 方法指定何時移除該任務以及結束所有與之相關的 Activity。
private Intent newDocumentIntent() { final Intent newDocumentIntent = new Intent(this, NewDocumentActivity.class); newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS); newDocumentIntent.putExtra(KEY_EXTRA_NEW_DOCUMENT_COUNTER, incrementAndGet()); return newDocumentIntent;}
要達到同樣的效果,請將 <activity> 屬性 android:autoRemoveFromRecents 設定為 false。文檔 Activity 的預設值為 true,常規 Activity 的預設值為 false。如前所述,使用此屬性替代 FLAG_ACTIVITY_RETAIN_IN_RECENTS 標誌。
demo的代碼
1.DocumentCentricActivity.java:
package com.example.documentcentricapps;import android.app.Activity;import android.content.Intent;import android.os.Build;import android.os.Bundle;import android.os.PersistableBundle;import android.util.Log;import android.view.View;import android.widget.CheckBox;public class DocumentCentricActivity extends Activity { private final static String TAG = "DocumentCentricActivity"; public final static String KEY_EXTRA_NEW_DOCUMENT_COUNTER = "KEY_EXTRA_NEW_DOCUMENT_COUNTER"; private static int mDocumentCounter = 0; private CheckBox mCheckbox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_document_centric_main); mCheckbox = (CheckBox) findViewById(R.id.multiple_task_checkbox); } @Override public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) { super.onPostCreate(savedInstanceState, persistentState); // Restore state from PersistableBundle if (persistentState != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mDocumentCounter = persistentState.getInt(KEY_EXTRA_NEW_DOCUMENT_COUNTER); } } } @Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { /* To maintain activity state across reboots the system saves and restore critical information for all tasks and their activities. Information known by the system includes the activity stack order, each task’s thumbnails and each activity’s and task's Intents. For Information that cannot be retained because they contain Bundles which can’t be persisted a new constrained version of Bundle, PersistableBundle is added. PersistableBundle can store only basic data types. To use it in your Activities you must declare the new activity:persistableMode attribute in the manifest. */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { outPersistentState.putInt(KEY_EXTRA_NEW_DOCUMENT_COUNTER, mDocumentCounter); } super.onSaveInstanceState(outState, outPersistentState); } public void createNewDocument(View view) { boolean useMultipleTasks = mCheckbox.isChecked(); final Intent newDocumentIntent = newDocumentIntent(); if (useMultipleTasks) { /* When {@linkIntent#FLAG_ACTIVITY_NEW_DOCUMENT} is used with {@link Intent#FLAG_ACTIVITY_MULTIPLE_TASK} the system will always create a new task with the target activity as the root. This allows the same document to be opened in more than one task. */ newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); } startActivity(newDocumentIntent); } /** * Returns an new {@link Intent} to start {@link NewDocumentActivity} as a new document in * overview menu. * * To start a new document task {@link Intent#FLAG_ACTIVITY_NEW_DOCUMENT} must be used. The * system will search through existing tasks for one whose Intent matches the Intent component * name and the Intent data. If it finds one then that task will be brought to the front and the * new Intent will be passed to onNewIntent(). * * Activities launched with the NEW_DOCUMENT flag must be created with launchMode="standard". */ private Intent newDocumentIntent() { final Intent newDocumentIntent = new Intent(this, NewDocumentActivity.class); newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); newDocumentIntent.putExtra(KEY_EXTRA_NEW_DOCUMENT_COUNTER, incrementAndGet()); return newDocumentIntent; } private static int incrementAndGet() { Log.d(TAG, "incrementAndGet(): " + mDocumentCounter); return mDocumentCounter++; }}
2.NewDocumentActivity.java:
public class NewDocumentActivity extends Activity { private TextView mDocumentCounterTextView; private int mDocumentCount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new_document); mDocumentCount = getIntent() .getIntExtra(DocumentCentricActivity.KEY_EXTRA_NEW_DOCUMENT_COUNTER, 0); mDocumentCounterTextView = (TextView) findViewById( R.id.hello_new_document_text_view); setDocumentCounterText(R.string.hello_new_document_counter); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); /* If {@link Intent#FLAG_ACTIVITY_MULTIPLE_TASK} has not been used this Activity will be reused. */ setDocumentCounterText(R.string.reusing_document_counter); } public void onRemoveFromOverview(View view) { // It is good pratice to remove a document from the overview stack if not needed anymore. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { finishAndRemoveTask(); } } public void setDocumentCounterText(int resId) { mDocumentCounterTextView .setText(String.format(getString(resId), String.valueOf(mDocumentCount))); }}
上邊只是主要的代碼,完整的demo下載:http://download.csdn.net/detail/jycboy/9715602
轉寄請註明出處:http://www.cnblogs.com/jycboy/p/overview_screen.html