簡體   English   中英

導航體系結構組件:如何從后堆棧導航片段?

[英]Navigation Architecture Component: How i can navigate a fragment from the back stack?

我有兩個片段A和B我回到A時使用導航控制器從A導航到B,導航控制器創建A的另一個實例! 不從堆棧中調用舊實例並增加麻袋大小,在這種情況下,堆棧大小將為3個片段,我該如何解決此問題?

這是我的graph.xml

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/home_dest"
app:startDestination="@id/home_dest">

<fragment
    android:id="@+id/a_dest"
    android:name="com.intcore.e_commerce.e_commerce.ui.homefragment.HomeFragment"
    android:label="@string/home"
    tools:layout="@layout/fragment_home" />
<fragment
    android:id="@+id/b_dest"
    android:name="com.intcore.e_commerce.e_commerce.ui.favoritesfragment.FavoritesFragment"
    android:label="@string/favorites"
    tools:layout="@layout/fragment_favorites" /></avigation>

這是我用來導航的代碼。

void onAClicked() {
    Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.a_dest);
}


void onBClicked() {
    Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.b_dest);
}
  1. 對於將nav_host_fragment連接到a_dest的操作,您需要將pop行為設置為popTo nav_host_fragment inclusive(在導航編輯器中)。
  2. 重復同樣的操作,將nav_host_fragment連接到b_dest。 PopTo inclusive彈出后台堆棧中的所有內容,包括引用的片段事務。
  3. 創建一個將a_dest連接到nav_host_fragment並將b_dest連接到nav_host_fragment的操作。 對於每個片段,請在關閉包含標志的情況下選擇PopTo nav_host_fragment。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM