編輯:關於 Android 程式設計
本示例演示如何通過設置 Intent 對象的標記(Flag),改變當前任務堆棧中既有 Activity 的順序,並給出一個完整的 Activity 堆棧管理類。
FLAG_ACTIVITY_BROUGHT_TO_FRONT
應用代碼中通常不設置此標記,而是由系統自動為單任務啟動模式的 Activity 設置。
FLAG_ACTIVITY_CLEAR_TASK
添加此標記後,Activity 啟動前會清除與它關聯的所有既有任務,使新 Activity 成為空任務的根,其他 Activity 均被銷毀。它需與 FLAG_ACTIVITY_NEW_TASK 聯合使用。
FLAG_ACTIVITY_CLEAR_TOP
設置此標記後,若要啟動的 Activity 已在當前任務中運行,則不會創建新實例,而是關閉堆棧中位於它之上的所有 Activity,再把新的 Intent 發送給這個既有 Activity。
FLAG_ACTIVITY_CLEAR_WHEN_TASK_REST
設置此標記後,當任務被復位時,堆棧中此標記點之後的 Activity 都應被清除。
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
設置此標記後,新的 Activity 不會被保留在最近啟動的列表中。
FLAG_ACTIVITY_FORWARD_RESULT
設置此標記後,若用於從既有 Activity 啟動新 Activity,會把回復目標轉移到新 Activity,使其能調用 setResult(int) 把結果返回給原始 Activity。
FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
此標記通常不由應用代碼設置,若是從歷史中啟動,系統會自動設置。
FLAG_ACTIVITY_MULTIPLE_TASK
除非實現自己的頂層啟動器,否則不使用此標記。
FLAG_ACTIVITY_NEW_TASK
設置此標記後,該 Activity 將成為歷史堆棧之上新任務的起點。
FLAG_ACTIVITY_NO_ANIMATION
設置此標記後,將阻止系統在 Activity 間切換時的動畫變換。
FALG_ACTIVITY_NO_HISTORY
設置此標記後,新的 Activity 不會被保留在歷史堆棧中。
FLAG_ACTIVITY_NO_USER_ACTION
設置此標記後,在新啟動的 Activity 掛起前,會阻止普通的 onUserLeaveHint() 回調。電話撥號或鬧鐘程序常用此標記啟動 Activity。
FLAG_ACTIVITY_PREVIOUS_IS_TOP
設置此標記後,若用於從既有 Activity 啟動新 Activity,這個 Activity 不能用於接受發送給頂層 Activity 的新 Intent,通常認為它會被立即終止。
FLAG_ACTIVITY_REORDER_TO_FRONT
設置此標記後,會把任務歷史堆棧中既有的 Activity 帶到前台。這正是本示例要重點演示的標記。
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
設置此標記後,若 Activity 在新任務中啟動、或在既有任務堆棧中被帶到頂層,它就會作為任務的前門來啟動。
FLAG_ACTIVITY_SINGLE_TOP
設置此標記後,若要啟動的 Activity 已在歷史堆棧頂層運行,則不會被再次啟動。
FLAG_ACTIVITY_TASK_ON_HOME
設置此標記後,會導致新啟動的任務被放到當前主 Activity 任務之上。
2.1 定義清單文件(AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="my.android.test" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".ReorderOnLaunch" 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=".ReorderTwo" /> <activity android:name=".ReorderThree" /> <activity android:name=".ReorderFour" /> </application> <uses-sdk android:minSdkVersion="9" /> </manifest>
2.2 定義字符串資源(strings.xml)
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, ReorderOnLaunch!</string> <string name="app_name">ReorderOnLaunch</string> <string name="reorder_on_launch">This is the first of a sequence of four Activities. A button on the fourth will use the Intent.FLAG_ACTIVITY_REORDER_TO_FRONT flag to bring the second of the activities to the front of the history stack. After that, proceeding back through the history should begin with the newly-frontmost second reorder activity, then the fourth, the third, and finally the first.</string> <string name="reorder_launch_two">Go to the second</string> <string name="reorder_two_text">This is the second in a sequence of four Activities.</string> <string name="reorder_launch_three">Go to the third</string> <string name="reorder_three_text">This is the third of a sequence of four Activities.</string> <string name="reorder_launch_four">Go to the fourth</string> <string name="reorder_four_text">This is the last in a sequence of four Activities.</string> <string name="reorder_second_to_front">Bring the second in front</string> </resources>
2.3 定義布局文件
reorder_on_launch.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0" android:paddingBottom="4dip" android:text="@string/reorder_on_launch"/> <Button android:id="@+id/reorder_launch_two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/reorder_launch_two" /> </LinearLayout>
reorder_two.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0" android:paddingBottom="4dip" android:text="@string/reorder_two_text"/> <Button android:id="@+id/reorder_launch_three" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/reorder_launch_three" /> </LinearLayout>
reorder_three.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0" android:paddingBottom="4dip" android:text="@string/reorder_three_text"/> <Button android:id="@+id/reorder_launch_four" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/reorder_launch_four" /> </LinearLayout>
reorder_four.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0" android:paddingBottom="4dip" android:text="@string/reorder_four_text"/> <Button android:id="@+id/reorder_second_to_front" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/reorder_second_to_front" /> </LinearLayout>
2.4 創建 Activity
ReorderOnLaunch.java
package my.android.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ReorderOnLaunch extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.reorder_on_launch); Button twoButton = (Button)findViewById(R.id.reorder_launch_two); twoButton.setOnClickListener(mClickListener); } private final OnClickListener mClickListener = new OnClickListener(){ public void onClick(View v){ startActivity(new Intent(ReorderOnLaunch.this, ReorderTwo.class)); } }; }
ReorderTwo.java
package my.android.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ReorderTwo extends Activity { @Override protected void onCreate(Bundle saveState){ super.onCreate(saveState); setContentView(R.layout.reorder_two); Button twoButton = (Button)findViewById(R.id.reorder_launch_three); twoButton.setOnClickListener(mClickListener); } private final OnClickListener mClickListener = new OnClickListener(){ publicvoid onClick(View v){ startActivity(new Intent(ReorderTwo.this, ReorderThree.class)); } }; }
ReorderThree.java
package my.android.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ReorderThree extends Activity { private final OnClickListener mClickListener = new OnClickListener(){ public void onClick(View v){ startActivity(new Intent(ReorderThree.this, ReorderFour.class)); } }; @Override protected void onCreate(Bundle saveState){ super.onCreate(saveState); setContentView(R.layout.reorder_three); Button twoButton = (Button)findViewById(R.id.reorder_launch_four); twoButton.setOnClickListener(mClickListener); } }
ReorderFour.java
package my.android.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; publicclass ReorderFour extends Activity { @Override protected void onCreate(Bundle saveState){ super.onCreate(saveState); setContentView(R.layout.reorder_four); Button twoButton = (Button)findViewById(R.id.reorder_second_to_front); twoButton.setOnClickListener(mClickListener); } private final OnClickListener mClickListener = new OnClickListener(){ public void onClick(View v){ Intent intent = new Intent(ReorderFour.this, ReorderTwo.class); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(intent); } }; }
可以看到,在 ReorderFour 中點擊按鈕時,通過 intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) 就能把 ReorderTwo 重新拉到堆棧前台。
實際開發中,我們通常需要一個統一的管理類來維護所有 Activity,方便隨時退出或關閉指定頁面。下面是一個典型的堆棧式管理實現:
package net.oschina.app; import java.util.Stack; import android.app.Activity; import android.content.Context; /** * activity堆棧式管理 */ public class AppManager { private static Stack<Activity> activityStack; private static AppManager instance; private AppManager() {} /** * 單一實例 */ public static AppManager getAppManager() { if (instance == null) { instance = new AppManager(); } return instance; } /** * 添加Activity到堆棧 */ public void addActivity(Activity activity) { if (activityStack == null) { activityStack = new Stack<Activity>(); } activityStack.add(activity); } /** * 獲取當前Activity(堆棧中最後一個壓入的) */ public Activity currentActivity() { Activity activity = activityStack.lastElement(); return activity; } /** * 結束當前Activity(堆棧中最後一個壓入的) */ public void finishActivity() { Activity activity = activityStack.lastElement(); finishActivity(activity); } /** * 結束指定的Activity */ public void finishActivity(Activity activity) { if (activity != null && !activity.isFinishing()) { activityStack.remove(activity); activity.finish(); activity = null; } } /** * 結束指定類名的Activity */ public void finishActivity(Class<?> cls) { for (Activity activity : activityStack) { if (activity.getClass().equals(cls)) { finishActivity(activity); break; } } } /** * 結束所有Activity */ public void finishAllActivity() { for (int i = 0, size = activityStack.size(); i < size; i++) { if (null != activityStack.get(i)) { finishActivity(activityStack.get(i)); break; } } activityStack.clear(); } /** * 獲取指定的Activity */ public static Activity getActivity(Class<?> cls) { if (activityStack != null) for (Activity activity : activityStack) { if (activity.getClass().equals(cls)) { return activity; } } return null; } /** * 退出應用程序 */ public void AppExit(Context context) { try { finishAllActivity(); // 殺死該應用進程 android.os.Process.killProcess(android.os.Process.myPid()); System.exit(0); } catch (Exception e) { } } }
以上就是關於 Android Activity 堆棧與管理的完整講解,希望對大家有所幫助,如有疑問歡迎留言交流。
Android launchMode 詳解:singleTask 與 singleInstance 實測對比
網路上介紹 Android launchMode 的文章不少,但關於 singleTask 與 singleInstance 這兩種模式,評論區一直有些爭議。本文用實例把
startActivityForResult 為何會讓 singleTop 失效?源碼層面解析
最近在讀 AMS 的程式碼時碰到一個疑問,起點是 startActivityUncheckedLocked 裡的這段: Slog.d(DDY, !!!!!!!!!!!!
MyHttpUtils:輕量異步網路請求框架使用指南
框架能解決什麼MyHttpUtils 是一個輕量的 Android 異步網路請求框架。開發者只需要傳入 url 與 JavaBean 型別,就能在回調方法中取得解析後的物
TraceView 使用全解:用圖形化視角揪出 Android 效能瓶頸
TraceView 是 Android 平台自帶的效能分析利器。它把方法級別的執行資訊以圖形方式鋪開,讓我們能直觀地看出時間究竟花在哪裡,而不是靠猜。 TraceView