0

I am using bottom navigation view with navigation components, when previous selected menu item is reselected fragment is reopening from oncreate

When I open my activity document fragment is loading and when I select even fragment and again reselect document fragment he previous state of document fragment is no loading, and document fragment is loading as new fragment.

I want to load previous state of fragment when I again select any of the fragment.

I don't know how to achieve this, any one have any idea about how should I approach this problem?

//my activity.kt
navController = findNavController(R.id.fragmentContainerView)
mViewDataBinding?.bottomNavigation?.setupWithNavController(navController)
//my activity.xml
 <fragment
            android:id="@+id/fragmentContainerView"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:defaultNavHost="true"
            app:navGraph="@navigation/menu_navigation"
            app:layout_constraintBottom_toTopOf="@id/bottomNavigation"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:labelVisibilityMode="labeled"
            android:background="@color/backgroundColor"
            app:itemBackground="@color/backgroundColor"
            app:menu="@menu/bottom_navigation_menu"
            app:itemIconTint="@color/bottom_nav_item_color"
            app:itemTextColor="@color/bottom_nav_item_color"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>
//navigation file
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/menu_navigation"
    app:startDestination="@id/documentsFragment">

    <fragment
        android:id="@+id/documentsFragment"
        android:name="packagename.DocumentsFragment"
        android:label="DocumentsFragment" />
    <fragment
        android:id="@+id/eventsFragment"
        android:name="packagename.EventsFragment"
        android:label="EventsFragment" />
    <fragment
        android:id="@+id/notesFragment"
        android:name="packagename.NotesFragment"
        android:label="NotesFragment" />
    <fragment
        android:id="@+id/tasksFragment"
        android:name="packagename.TasksFragment"
        android:label="TasksFragment" />
    <fragment
        android:id="@+id/menuFragment"
        android:name="packagename.MenuFragment"
        android:label="MenuFragment" />
</navigation>

what I want is to load fragment(state) where the user left previously.

Lino
  • 5,084
  • 3
  • 21
  • 39
Ajay
  • 11
  • 1

2 Answers2

0
  • As I know, there are no way to reopen fragment when you re-select it from BottomNavigation
  • I did use NavigationComponent with BottomNavigationView in my projects. And to deal with it, I think you should consider using Fragment with ViewModel to keep the data-state. Then, when you re-select it, only the fragment's view is recreated.
fshdn19
  • 374
  • 1
  • 8
0

Navigational Components supports Multiple backstack. In your case,

  1. You have to make your home fragment as a starting destination
  2. Add an item Click listener and set it up with navController as follow.

Listener

binding.bottomNavigation.setOnItemSelectedListener {
    it.onNavDestinationSelected(navController)
}
Sohaib Ahmed
  • 1,990
  • 1
  • 5
  • 23