Home > Software design >  Bottom navigation view using navigation component
Bottom navigation view using navigation component

Time:12-30

I have a problem with bottom nav view. I have a 3 fragments: MoviesFragment(Home Fragment), MovieInfoFragment and Profile fragment. It work when a navigate using bottom nav view MoviesFragment->ProfileFragment ProfileFragment->MoviesFragment but not work when i navigate from MovieInfoFragment to MoviesFragment, i have no idea why. Pleashe help

bottom_navigation_menu

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@ id/moviesFragment"
        android:icon="@drawable/ic_home"
        android:title="@string/home" />

    <item
        android:id="@ id/profileFragment"
        android:icon="@drawable/ic_profile"
        android:title="@string/profile" />

</menu>

nav_graph

<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/nav_graph"
    app:startDestination="@id/moviesFragment">

    <fragment
        android:id="@ id/moviesFragment"
        android:name="com.sideki.imdb_app.ui.movies_list.MoviesFragment"
        android:label="fragment_movies"
        tools:layout="@layout/fragment_movies">

        <action
            android:id="@ id/to_movieInfoFragment"
            app:destination="@id/movieInfoFragment" />
    </fragment>

    <fragment
        android:id="@ id/movieInfoFragment"
        android:name="com.sideki.imdb_app.ui.movie_info.MovieInfoFragment"
        android:label="MovieInfoFragment">

        <argument
            android:name="movieId"
            app:argType="string" />
    </fragment>
    <fragment
        android:id="@ id/profileFragment"
        android:name="com.sideki.imdb_app.data.api.ProfileFragment"
        android:label="ProfileFragment" />
</navigation>

class MainActivity : FragmentActivity() {

private lateinit var binding: ActivityMainBinding
private lateinit var navController: NavController

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)
    val navHostFragment = supportFragmentManager.findFragmentById(R.id.fragmentNavHost)
    navController = navHostFragment!!.findNavController()
    binding.bottomNavigation.setupWithNavController(navController)
}

Maybe a problem hides in backstack?

CodePudding user response:

I think the issue when you navigate from the MovieInfoFragment to the MoviesFragment, the MovieInfoFragment is still on the back stack, so pressing the back button will take the user back to the MovieInfoFragment rather than the MoviesFragment.

Try to use popBackStack() when you navigate to the MoviesFragment.

CodePudding user response:

You should have 3 items in your bottom_navigation_menu, connected to 3 fragments - in your code you have only 2. IDs of those items should be the same as IDs of destination fragments in nav_graph.

nav_graph looks OK, although specifying actions is not needed for bottom navigation. You only need to add 3rd item in bottom_navigation_menu. Something like this:


    <item
        android:id="@ id/moviesFragment"
        android:icon="@drawable/ic_home"
        android:title="@string/home" />

    <item
        android:id="@ id/movieInfoFragment"
        android:icon="@drawable/ic_some_name"
        android:title="@string/some_title_res" />

    <item
        android:id="@ id/profileFragment"
        android:icon="@drawable/ic_profile"
        android:title="@string/profile" />

</menu>
  • Related