[英]Removing and adding activities to the back stack
在導航抽屜的 “交叉導航到較低層次結構級別之后的系統返回”部分中,他們說:
如果用戶從導航抽屜導航到較低層次的屏幕並且屏幕具有直接父級,則重置后退堆棧並且后退指向目標屏幕的父級。 此返回行為與用戶從通知導航到應用程序時的行為相同。
我知道可以通過使用FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_NEW_TASK啟動活動來重置后台堆棧,但這似乎在這里不可用,因為它不會為Lower 1.1.1創建后台堆棧。
知道如何從堆棧中刪除TopView2,同時在啟動Lower 1.1.1時添加TopView1 - > Lower 1.1返回堆棧嗎? 考慮到導航抽屜文檔中提到的這一點,我期待一個簡單的解決方案。
編輯合成版本:
1)在清單文件中聲明應用程序的分層導航結構。
2)應用程序的根活動應在應用程序層次結構中的TopView之間執行視圖切換。*
3)層次結構中較低的活動應執行“選擇性向上”導航。
* 重要提示:當交易用於水平導航時(例如切換選項卡或導航抽屜頂視圖時),不應將事務添加到后台堆棧。
詳細描述:
出於以下原因,您應該避免將Intent Flags與導航抽屜等新導航模式一起使用 :
launchMode
重疊/沖突。 相反,請選擇新的Navigation API:
<activity>
指定的分層元數據。 NavUtils
為早期的Android版本提供了相同的功能。 TaskStackBuilder
為跨任務導航提供了額外的實用程序。 所以回答你的問題,一般的想法是:
1)您需要使用android:parentActivityName
屬性(和相應的<meta-data>
元素)聲明清單文件中每個活動的邏輯父代,如:
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myapp.RootDrawerActivity" ...>
...
</activity>
<!-- A child of the root activity -->
<activity
android:name="com.example.myapp.Lower11 "
android:label="@string/lower11"
android:parentActivityName="com.example.myapp.RootDrawerActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myapp.RootDrawerActivity" />
</activity>
<activity
android:name="com.example.myapp.Lower111 "
android:label="@string/lower111"
android:parentActivityName="com.example.myapp.Lower11" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myapp.Lower11" />
</activity>
</application>
2)在根Activity中,抽屜項目選擇應通過替換Activity的當前片段內容來啟動“視圖切換”操作。
視圖切換遵循與列表或選項卡導航相同的基本策略,因為視圖切換不會創建導航歷史記錄 。 此模式應僅用於任務的根活動,使某種形式的向上導航對導航層次結構中的活動保持活動狀態(在您的情況下為低1.1和低1.1.1)。 這里重要的是你不需要從堆棧中刪除TopView2,而是在將視圖(或片段id)的位置作為額外的位置傳遞之前執行視圖切換。
在你的root Activity中做這樣的事情:
@Override
protected void onDrawerItemSelected(int position) {
// Update the main content by replacing fragments
CategoryFragment fragment = new CategoryFragment();
Bundle args = new Bundle();
args.putInt(RootDrawerActivity.ARG_SORT, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
// Update selected item and title, then close the drawer
setDrawerItemChecked(position, true);
setTitle(getResources().getStringArray(R.array.drawer_array)[position]);
closeDrawer();
}
3)然后在層次結構中降低(即Lower1.1),您應該執行“選擇性向上”導航, 在過程中重新創建任務堆棧 。
選擇性向上允許用戶隨意跳過應用程序的導航層次結構。 應用程序應該對此進行處理,因為它會使用TaskStackBuilder或類似工具替換當前任務堆棧 (這是您想要的!)。 這是導航抽屜的唯一形式,應該在任務的根活動之外使用。
@Override
protected void onDrawerItemSelected(int position) {
TaskStackBuilder.create(this)
.addParentStack(RootDrawerActivity.class)
.addNextIntent(new Intent(this, RootDrawerActivity.class)
.putExtra(RootDrawerActivity.ARG_SORT, position))
.startActivities();
}
參考文獻:
http://developer.android.com/training/implementing-navigation/ancestral.html https://speakerdeck.com/jgilfelt/this-way-up-implementing-effective-navigation-on-android
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.