[英]Problem with Android Navigation: Multiple back stacks
My problem is when I try to change the fragment with the bottom navigation it works except in one case.我的问题是,当我尝试使用底部导航更改片段时,它可以工作,但在一种情况下除外。 I have 5 fragments, when I enter in the 3rd and after go to the 5th, the icon of the 5th don´t change it color.我有 5 个碎片,当我进入第 3 和进入第 5 后,第 5 的图标不会改变它的颜色。 After that when I do another navigation the APP crash and give me the next error:之后,当我进行另一次导航时,APP 崩溃并给我下一个错误:
java.lang.IndexOutOfBoundsException: fromIndex = -1
at java.util.ArrayList.subListRangeCheck(ArrayList.java:1014)
at java.util.ArrayList.subList(ArrayList.java:1008)
at androidx.navigation.fragment.FragmentNavigator.popBackStack(FragmentNavigator.kt:80)
at androidx.navigation.NavController.popBackStackInternal(NavController.kt:275)
at androidx.navigation.NavController.popBackStackInternal(NavController.kt:558)
at androidx.navigation.NavController.navigate(NavController.kt:1682)
at androidx.navigation.NavController.navigate(NavController.kt:1541)
at androidx.navigation.NavController.navigate(NavController.kt:1468)
at androidx.navigation.ui.NavigationUI.onNavDestinationSelected(NavigationUI.kt:92)
at androidx.navigation.ui.NavigationUI.setupWithNavController$lambda-6(NavigationUI.kt:602)
at androidx.navigation.ui.NavigationUI.$r8$lambda$6wzEv9QqEZKdQFS1sQQy-bdQvgE(Unknown Source:0)
at androidx.navigation.ui.NavigationUI$$ExternalSyntheticLambda2.onNavigationItemSelected(Unknown Source:2)
at com.google.android.material.navigation.NavigationBarView$1.onMenuItemSelected(NavigationBarView.java:295)
at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834)
at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985)
at com.google.android.material.navigation.NavigationBarMenuView$1.onClick(NavigationBarMenuView.java:133)
at android.view.View.performClick(View.java:7520)
at android.view.View.performClickInternal(View.java:7489)
at android.view.View.access$3600(View.java:826)
at android.view.View$PerformClick.run(View.java:28555)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:233)
at android.app.ActivityThread.main(ActivityThread.java:8010)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
But if previously i clicked in the 5th fragment and go to the 3rd and after 5th again it works correctly.但是如果之前我点击了第 5 个片段并转到第 3 个片段,然后在第 5 个片段之后再次正常工作。
For my code I follow this tutorial and the code are the same in both cases.对于我的代码,我遵循本教程,两种情况下的代码都是相同的。
edit: Here is my code to add the navigation in MainActivity:编辑:这是我在 MainActivity 中添加导航的代码:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
if(savedInstanceState == null){
setupBottomNavigationBar()
}
private fun setupBottomNavigationBar(){
val graphs = setOf(
R.id.firstFragment,
R.id.secondFragment,
R.id.thirdFragment,
R.id.fourthFragment,
R.id.fithFragment
)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_container) as NavHostFragment
currentNavController = navHostFragment.navController
val bottomNavigation = findViewById<BottomNavigationView>(R.id.bottom_navigation)
bottomNavigation.setupWithNavController(currentNavController)
appBarConfiguration = AppBarConfiguration(graphs)
}
activity_main.xml activity_main.xml
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activities.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/super_nav"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_nav"
app:itemIconTint="@drawable/botton_navigation_colors"
app:labelVisibilityMode="unlabeled"
app:itemIconSize="35dp"
/>
bottom_nav.xml bottom_nav.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/first_navigation"
android:icon="@drawable/ic_first_black"
android:contentDescription="@string/first_desc"
android:title="@string/first_title" />
<item
android:id="@+id/second_navigation"
android:icon="@drawable/ic_second"
android:contentDescription="@string/second_desc"
android:title="@string/second_title" />
<item
android:id="@+id/third_navigation"
android:icon="@drawable/ic_third"
android:contentDescription="@string/third_desc"
android:title="@string/third_title" />
<item
android:id="@+id/fourth_navigation"
android:icon="@drawable/ic_fourth"
android:contentDescription="@string/fourth_desc"
android:title="@string/fourth_title" />
<item
android:id="@+id/fifth_navigation"
android:icon="@drawable/ic_fifth"
android:contentDescription="@string/fifth_desc"
android:title="@string/fifth_title" />
And super_nav.xml和 super_nav.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/super_nav"
app:startDestination="@+id/main_navigation">
<include app:graph="@navigation/first_navigation"/>
<include app:graph="@navigation/second_navigation"/>
<include app:graph="@navigation/third_navigation" />
<include app:graph="@navigation/fourth_cart_navigation" />
<include app:graph="@navigation/fifth_navigation" />
I've just fixed very similar issue.我刚刚解决了非常相似的问题。 The problem in my case was that I had destinations with the same id in several navigation subgraphs.就我而言,问题是我在几个导航子图中有相同 id 的目的地。 I mean:我是说:
<include app:graph="@navigation/first_sub_graph"/>
<include app:graph="@navigation/second_sub_graph"/>
<navigation
android:id="@+id/first_sub_graph"
..>
...
<fragment
android:id="@+id/**destinationID_1**"
...
/>
</navigation>
<navigation
android:id="@+id/second_sub_graph"
..>
...
<fragment
android:id="@+id/**destinationID_1**"
...
/>
</navigation>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.