Reputation: 81
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. After that when I do another navigation the APP crash and give me the next error:
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.
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:
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
<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
<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
<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" />
Upvotes: 6
Views: 1600
Reputation: 343
I was getting this with 2.5.3 navigation library but works fine with latest (2.7.2)
Upvotes: 0
Reputation: 138
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. 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>
Upvotes: 4