admin管理员组文章数量:1419168
I have a MainActivity with a navigation graph which has two fragments say FragmentA and FragmentBottomNav. When click on a button in FragmentA the app navigates from FragmentA to FragmentBottomNav.
The FragmentBottomNav contains a BottomNavigationView and it has a separate navigation graph with 2 fragments FragmentB and FragmentC.
Everything works fine util this.When I press back button from FragmentBottomNav fragment it navigates back to FragmentA, but when I navigate to FragmentBottomNav again from FragmentA, The FragmentB and FragmentC are not loading.
Please find the Navigation graph and code snippets below.
main_navigation_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android=";
xmlns:app=";
xmlns:tools=";
android:id="@+id/nav_test"
app:startDestination="@id/fragmentA">
<fragment
android:id="@+id/fragmentA"
android:name="com.test.presentation.test.FragmentA"
android:label="FragmentA"
tools:layout="@layout/fragment_a">
<action
android:id="@+id/action_fragmentA_to_BNavTestFragment"
app:destination="@id/BNavTestFragment" />
</fragment>
<fragment
android:id="@+id/BNavTestFragment"
android:name="com.test.presentation.test.BottomNavTestFragment"
android:label="BNavTestFragment"
tools:layout="@layout/fragment_b_nav_test" />
</navigation>
bottom_nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android=";
xmlns:app=";
xmlns:tools=";
android:id="@+id/nav_test_bottom"
app:startDestination="@id/orderFragment">
<fragment
android:id="@+id/orderFragment"
android:name="com.test.presentation.test.FragmentB"
android:label="FragmentB"
tools:layout="@layout/fragment_b" />
<fragment
android:id="@+id/profileFragment"
android:name="com.test.presentation.test.FragmentC"
android:label="FragmentC"
tools:layout="@layout/fragment_c" />
</navigation>
fragment_layout_of_bottom_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=";
xmlns:app=";
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E0668F">
<!-- Fragment Container for Navigation -->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_member_bottom_nav"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_test_bottom_view" />
<!-- BottomNavigationView -->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:backgroundTint="@color/primary_variant"
app:itemIconTint="@color/bottom_nav_item_color"
app:itemTextColor="@color/bottom_nav_item_text_color"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav_menu_test" />
</androidx.constraintlayout.widget.ConstraintLayout>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android=";>
<item
android:id="@+id/orderFragment"
android:icon="@drawable/ic_bottom_menu_orders"
android:title="@string/bt_menu_orders" />
<item
android:id="@+id/profileFragment"
android:icon="@drawable/ic_bottom_menu_profile"
android:title="@string/bt_menu_profile" />
</menu>
//inside BottomNavFragment.kt
private fun setUpBottomNavigation() {
val navHostFragment = requireActivity().supportFragmentManager
.findFragmentById(R.id.nav_host_member_bottom_nav) as NavHostFragment
val navController: NavController = navHostFragment.navController
val bottomNavigationView =
requireView().findViewById<BottomNavigationView>(R.id.bottom_navigation)
bottomNavigationView.setupWithNavController(navController)
}
I have a MainActivity with a navigation graph which has two fragments say FragmentA and FragmentBottomNav. When click on a button in FragmentA the app navigates from FragmentA to FragmentBottomNav.
The FragmentBottomNav contains a BottomNavigationView and it has a separate navigation graph with 2 fragments FragmentB and FragmentC.
Everything works fine util this.When I press back button from FragmentBottomNav fragment it navigates back to FragmentA, but when I navigate to FragmentBottomNav again from FragmentA, The FragmentB and FragmentC are not loading.
Please find the Navigation graph and code snippets below.
main_navigation_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android/apk/res/android"
xmlns:app="http://schemas.android/apk/res-auto"
xmlns:tools="http://schemas.android/tools"
android:id="@+id/nav_test"
app:startDestination="@id/fragmentA">
<fragment
android:id="@+id/fragmentA"
android:name="com.test.presentation.test.FragmentA"
android:label="FragmentA"
tools:layout="@layout/fragment_a">
<action
android:id="@+id/action_fragmentA_to_BNavTestFragment"
app:destination="@id/BNavTestFragment" />
</fragment>
<fragment
android:id="@+id/BNavTestFragment"
android:name="com.test.presentation.test.BottomNavTestFragment"
android:label="BNavTestFragment"
tools:layout="@layout/fragment_b_nav_test" />
</navigation>
bottom_nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android/apk/res/android"
xmlns:app="http://schemas.android/apk/res-auto"
xmlns:tools="http://schemas.android/tools"
android:id="@+id/nav_test_bottom"
app:startDestination="@id/orderFragment">
<fragment
android:id="@+id/orderFragment"
android:name="com.test.presentation.test.FragmentB"
android:label="FragmentB"
tools:layout="@layout/fragment_b" />
<fragment
android:id="@+id/profileFragment"
android:name="com.test.presentation.test.FragmentC"
android:label="FragmentC"
tools:layout="@layout/fragment_c" />
</navigation>
fragment_layout_of_bottom_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android/apk/res/android"
xmlns:app="http://schemas.android/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E0668F">
<!-- Fragment Container for Navigation -->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_member_bottom_nav"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_test_bottom_view" />
<!-- BottomNavigationView -->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:backgroundTint="@color/primary_variant"
app:itemIconTint="@color/bottom_nav_item_color"
app:itemTextColor="@color/bottom_nav_item_text_color"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav_menu_test" />
</androidx.constraintlayout.widget.ConstraintLayout>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android/apk/res/android">
<item
android:id="@+id/orderFragment"
android:icon="@drawable/ic_bottom_menu_orders"
android:title="@string/bt_menu_orders" />
<item
android:id="@+id/profileFragment"
android:icon="@drawable/ic_bottom_menu_profile"
android:title="@string/bt_menu_profile" />
</menu>
//inside BottomNavFragment.kt
private fun setUpBottomNavigation() {
val navHostFragment = requireActivity().supportFragmentManager
.findFragmentById(R.id.nav_host_member_bottom_nav) as NavHostFragment
val navController: NavController = navHostFragment.navController
val bottomNavigationView =
requireView().findViewById<BottomNavigationView>(R.id.bottom_navigation)
bottomNavigationView.setupWithNavController(navController)
}
Share
Improve this question
edited Jan 29 at 8:10
darwin
asked Jan 29 at 7:56
darwindarwin
1,5941 gold badge23 silver badges33 bronze badges
1 Answer
Reset to default 0when navigating back to FragmentBottomNav from FragmentA, you need to ensure that the BottomNavigationView and its associated navigation graph are properly re-initialized when FragmentBottomNav is reloaded.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navController = findNavController(R.id.nav_host_fragment)
setupActionBarWithNavController(navController)
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return navController.navigateUp() || super.onSupportNavigateUp()
}
}
Fragment
class FragmentBottomNav : Fragment(R.layout.fragment_bottom_nav) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val navHostFragment = childFragmentManager.findFragmentById(R.id.bottom_nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
val bottomNavigationView = view.findViewById<BottomNavigationView>(R.id.bottom_navigation_view)
bottomNavigationView.setupWithNavController(navController)
}
}
And we also have OnNavigationItemReselectedListener to handle reselection,so can also be managed by this
bottomNavigationView.setOnNavigationItemReselectedListener { item ->
when (item.itemId) {
R.id.fragmentB -> {
// Handle reselection of FragmentB
// For example, refresh the fragment
val navHostFragment = childFragmentManager.findFragmentById(R.id.bottom_nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
navController.popBackStack(R.id.fragmentB, false)
navController.navigate(R.id.fragmentB)
}
R.id.fragmentC -> {
// Handle reselection of FragmentC
// For example, refresh the fragment
val navHostFragment = childFragmentManager.findFragmentById(R.id.bottom_nav_host_fragment) as NavHostFragment
// or you can refresh here or make it start destination and pop back fragment, your call
}
}
}
本文标签: kotlinAndroid Navigation graph with Bottom Navigation view not working properlyStack Overflow
版权声明:本文标题:kotlin - Android Navigation graph with Bottom Navigation view not working properly - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745305802a2652636.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论