[英]Android Jetpack Navigation component issue with splash fragment as root destination
假設我們有 3 個帶有向上按鈕功能的工具欄的片段
SplashFragment -> FragmentA -> FragmentB
這是應用程序的預期流程。 當用戶按下后退按鈕到達 FragmentB 時,他應該轉到 FragmentA,再次按下后退按鈕時,應用程序應該退出。 我們如何使用導航組件實現這種行為? 我嘗試使用app:popUpTo
在action
標簽中執行此action
,不知何故我設法使其適用於硬件后退按鈕,但向上按鈕的行為沒有改變。
在這方面需要幫助。
警告:這在 Navigation alpha08 上已被棄用, 鏈接是當前的解決方案。
這可能就是你想要的答案。 使用app:clearTask="true"
<fragment
android:id="@+id/splashFragment"
android:name="xxx.SplashFragment"
android:label="fragment_splash"
tools:layout="@layout/fragment_splash">
<action
android:id="@+id/action_splashFragment_to_mainFragment"
app:destination="@id/mainFragment"
app:enterAnim="@anim/anim_right_in"
app:exitAnim="@anim/anim_left_out"
app:popEnterAnim="@anim/anim_left_in"
app:popExitAnim="@anim/anim_right_out"
app:clearTask="true"/>
您可以嘗試這樣做:
App 啟動 Fragment-A 后,清除 backstack ie; SplashFragment ,現在當用戶向前導航時,一切都相同,但在返回 Fragment-A 后,如果他點擊后退按鈕應用程序退出。
希望這可以幫助!
我有一個類似的流程,什么有效,我相信一個干凈的解決方案是讓你的FragmentA
作為導航圖的起點(它總是在午餐時放在 backstack 的頂部)然后你想要導航(也許有條件地)到您的FragmentA
的onStart
(或生命周期的更早)上的SplashFragment
,最后當您希望 SplashFragment 消失時,您可以使用findNavController().popBackStack()
彈出片段。 那么只有 Fragment A 和 B 將保留在您的堆棧中,並且返回將按預期工作。
希望這可以幫助。
這是我的解決方案。 fragmentA->fragmentB->fragmentc
在后面,按鈕從 FragmentC 按下到 FragmentB,然后再次按下,你想退出表單應用程序。 將此行添加到 FragmentA 操作。
app:launchSingleTop="true"
app:popUpTo="@+id/fragmentA"
app:popUpToInclusive="true"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.