Navigation 踩坑記錄

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


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容