我正在嘗試解決BottomNavigationView使用 Jetpack Navigation 的一些導航問題。我正在使用 Jetpack 導航2.4.0-beta02。第一個問題是后退按鈕總是將我導航回起始目的地。這個是通過添加來解決的menuCategory: secondary。然而,還有另一個問題。
假設我有 4 個導航選單BottomNavigationView,片段 A、B、C 和 D。然后,還有另一個片段 A1,它不是BottomNavigationView. 導航圖如下所示:
<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/mobile_navigation"
app:startDestination="@ id/navigation_a">
<fragment
android:id="@ id/navigation_a"
android:name="com.example.FragmentA"
android:label=""
tools:layout="@layout/fragment_a">
<action
android:id="@ id/action_navigation_a_to_navigation_a1"
app:destination="@id/navigation_a1"
app:launchSingleTop="true" />
</fragment>
<fragment
android:id="@ id/navigation_a1"
android:name="com.example.FragmentA1"
android:label=""
tools:layout="@layout/fragment_a1" />
<fragment
android:id="@ id/navigation_b"
android:name="com.example.FragmentB"
android:label=""
tools:layout="@layout/fragment_b" />
<fragment
android:id="@ id/navigation_c"
android:name="com.example.FragmentC"
android:label=""
tools:layout="@layout/fragment_c" />
<fragment
android:id="@ id/navigation_d"
android:name="com.example.FragmentD"
android:label=""
tools:layout="@layout/fragment_d" />
</navigation>
因此,如果我從片段 A 導航到 A1,然后導航到片段 B(現在活動選單指示器位于第二個選單),然后我按下硬體后退按鈕,它會顯示正確的片段 A1。但是,活動選單指示器仍位于第二個選單而不是第一個選單。我期望第一個選單中的活動選單指示器,因為我從片段 A 導航到片段 A1。
這是我的menu.xml檔案
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@ id/navigation_a"
android:title="A"
android:menuCategory="secondary" />
<item
android:title="B"
android:id="@ id/navigation_b"
android:menuCategory="secondary" />
<item
android:title="C"
android:id="@ id/navigation_c"
android:menuCategory="secondary" />
<item
android:title="D"
android:id="@ id/navigation_d"
android:menuCategory="secondary" />
</menu>
和 MainActivity.kt
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment
navController = navHostFragment.navController
binding.navView.setupWithNavController(navController)
謝謝您的幫助!
uj5u.com熱心網友回復:
現在BottomNavigationView有 4 個片段:A、B、C 和 D。
你想讓導航像:
A (A highlighted) -> A1 -> B (A highlighted) -> Back pressed -> A1 (A highlighted)
但是你得到了:
A (A highlighted) -> A1 -> B (A highlighted) -> Back pressed -> A1 (B highlighted)
因此,當您從 B 回傳時,您需要突出顯示 A。
要做到這一點,您需要在片段 B 處按下后退時檢查后退堆疊中的前一個片段。
偽代碼:
// At fragment B
if (back_pressed) {
if (previous_fragment is A1) {
popup the back stack twice to return back to A
} else {
popup the back stack once (Normal behavior of the back stack)
}
}
代碼:
class FragmentB : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_b, container, false)
requireActivity().onBackPressedDispatcher.addCallback(
viewLifecycleOwner,
object : OnBackPressedCallback(true) {
override
fun handleOnBackPressed() { // Back pressed
val previousFragment = findNavController().previousBackStackEntry?.destination?.id
previousFragment?.let {
when (previousFragment) {
R.id.navigation_a1 ->
findNavController().navigateUp()
else ->
Log.d(TAG, "onCreateView: Not A1")
}
findNavController().navigateUp() // Normal behaviour when back is pressed
}
}
})
return view
}
}
邊注:
這將適用于當前navGraph設定;但我鼓勵您在當前圖表中使用另一個圖表,甚至嵌套導航;這樣您就可以將片段 A1 與片段分開BottomNavView。
更新:
我想按回時,它仍然顯示片段 A1 并且指示器位于第一個選單。相反,您的解決方案將我導航到片段 A。
為此,您通常只彈出回傳堆疊一次,并設定BottomNavView要檢查的選單項:
假設BottomNavView僅由活動托管,因此您可以使用requireActivity() as MainActivity; 將其轉換為您的活動名稱。
或者,如果它由片段托管,則使用parentFragment as MyParentFragment,類似地將其轉換為父片段的名稱。
在活動或父片段中:
fun highlightAItem() {
// Highlight A item from the bottomNavView
val item = navView.menu.findItem(R.id.navigation_a)
item.isChecked = true
}
在片段 B:
requireActivity().onBackPressedDispatcher.addCallback(
viewLifecycleOwner,
object : OnBackPressedCallback(true) {
override
fun handleOnBackPressed() {
// Highlight A item from the BottomNavView
(requireActivity() as MainActivity).highlightAItem()
// Pop backstack once to return to A1 fragment
findNavController().navigateUp()
}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/371987.html
標籤:安卓 android-fragments android-架构-组件 android-jetpack-导航 android-bottomnavigationvi
