destination顧名思義是導航的目的地,popUpTo(tag) 跳轉到tag,並彈出tag之上的fragment,popUpToInclusive=true會彈出tag,false則不會彈出。
應用場景,Fragment A -> B -> C.
1.正常導航:B導航到C,C返回時回到B。在fragmentB中調用該action
<action
android:id="@+id/action_FragmentB_to_FragmentC"
app:destination="@id/FragmentC"
/>
2.B導航C,C返回時回到A
方法1:在B中調用該action時,雖然會導航到C,但此時Fragment棧中B已經被彈出了(此時C不知道是不是在棧中),所以當C返回時會直接回到A。
<action
android:id="@+id/action_FragmentB_to_FragmentC"
app:destination="@id/FragmentC"
app:popUpTo="@+id/FragmentB"
app:popUpToInclusive="true"
/>
方法2: B到C正常導航,但在C中返回時通過直接導航到A同時設置popUpto爲A,popUpToInclusive爲false
<action
android:id="@+id/action_FragmentC_to_FragmentA"
app:destination="@id/FragmentA"
app:popUpTo="@+id/FragmentA"
app:popUpToInclusive="false"
/>
3. 在start destination的fragment中調用popBackStack()(一般把start destination的fragment作爲啓動頁,用完就彈出)會導致之後的fragment導航時找不到id的錯誤。官方是說不能彈出start destination的fragment。不過經過測試,在start destination的fragment的action中通過上述場景2的方法一方法,將popUpTo設爲自己,popUpToInclusive爲true時能夠在棧彈出自己。不過暫時不清楚這種方法是否有別的隱患。
以上是自己使用時的一點心得,有問題歡迎指正
補充一片他人的進階知識:https://www.jianshu.com/p/8abff4381585