1. 解決 navigate 重建 fragment 的一個(gè)可行辦法
1). 在 navigate 時(shí)傳入 NavOptions,設(shè)置 singleTop 。
navController.navigate(id, null, NavOptions.Builder().apply {
????setLaunchSingleTop(true)
}.build())
2). 在 xml 中?action 節(jié)點(diǎn)下設(shè)置 singleTop 屬性。
<action???
????android:id="@+id/action_a"????
????app:destination="@id/a"????
????app:singleTop="true" />
2. popUpTo 和?popUpToInclusive 屬性
<action
????android:id="@+id/action_a"
????app:destination="@id/a"
????app:popUpTo="@id/b"
????app:popUpToInclusive="true" />
popUpTo 表示該 action 在導(dǎo)航到?destination 所指定的 fragment 時(shí),先將在棧中該屬性對(duì)應(yīng)的 fragment 之上的 fragment 彈出,再實(shí)例化?destination fragment 并壓入棧中,popUpToInclusive 為 true 則表示?popUpTo 對(duì)應(yīng)的 fragment 也一并彈出。
如:棧中存在 A、B、C 三個(gè) fragment,入棧順序?yàn)?:A -> B -> C
此時(shí) navigate(R.id.action_a) 后棧順序?yàn)椋篈1 -> A2
若同時(shí)設(shè)置 singleTop 為 true,則棧順序?yàn)椋篈2
