標題頁被多個分支頁返回,每次返回都創建新實例,導致棧中存在多個標題頁。
<activity
android:name=".TitlePage"
android:launchMode="singleTop" /> <!-- 或 singleTask -->
singleTop:棧頂已存在實例則複用,觸發 onNewIntent。singleTask:全局單例,同一任務內只有一個實例。Intent intent = new Intent(this, TitlePage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); // 將現有實例移至棧頂
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // 清除目標之上的 Activity
startActivity(intent);
若分支頁無需保留歷史:
<activity android:name=".BranchPage" android:noHistory="true" />
按返回鍵直接銷毀,回到標題頁不創建新實例。
標題頁用 singleTop + FLAG_ACTIVITY_CLEAR_TOP|SINGLE_TOP,分支頁用 noHistory,棧管理最清晰。