Android Navigation component multiple Nav host

Viewed 272

I use single Activity patern in my project.In one of the fragments, let's call it ExploreFragment(), I need to create a FragmentContainerView where several fragments will change. The ExploreFragment() has a search engine and by default displays a MovieGanresFragment() with a list of genres. If I start typing text into the search engine, the fragment changes to MovieByNameFragment() where a list of movies with the name entered by the user is displayed. The question is, how do I implement ExploreFragment() where there will be a container with several fragments and all this using the Navigation component?

I decided to create a ExploreFragment() in the main graph and create a FragmentContainerView in it to switch the two fragments MovieGanresFragment() and MovieByNameFragment() . For switching fragments in ExploreFragment() I use childFragmentManager

 private fun switchFragment(movieName: String) {

        val searchMovieByNameFragment =   SearchMovieByNameFragment.newInstanceSearchMovieByNameFragment(movieName)

        openScreenWithMovies(searchMovieByNameFragment)

        childFragmentManager.beginTransaction()
            .replace(
                R.id.fragment_container_explore,
                searchMovieByNameFragment
            )
            .addToBackStack(null)
            .commit()
    }

It works, but there was another question. How to navigate from the MovieByNameFragment() fragment which is not in the main graph to the MoieDetailsFragment() fragment which is in the main graph and pass the data there?

Thsi is navigation XML

<?xml version="1.0" encoding="utf-8"?>
<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/mobile_navigation"
    app:startDestination="@id/welcome_screen">

    <fragment
        android:id="@+id/moviesFragment"
        android:name="com.example.movies.ui.movies.MoviesFragment"
        android:label="Home"
        tools:layout="@layout/fragment_movies">
        <action
            android:id="@+id/action_moviesFragment_to_movieDetailsFragment"
            app:destination="@id/movieDetailsFragment"
            app:enterAnim="@anim/enter_from_right"
            app:exitAnim="@anim/exit_to_right" />
        <action
            android:id="@+id/action_homeFragment_to_moviesFilterFragment"
            app:destination="@id/moviesFilterFragment"
            app:enterAnim="@anim/enter_from_top"
            app:exitAnim="@anim/exit_to_bottom" />

    </fragment>
    <fragment
        android:id="@+id/movieDetailsFragment"
        android:name="com.example.movies.ui.move_details.MovieDetailsFragment"
        tools:layout="@layout/fragment_movie_details" >
        <argument
            android:name="movieId"
            app:argType="integer"/>
        <argument
            android:name="showSavedIcon"
            app:argType="boolean"/>
        <action
            android:id="@+id/action_movieDetailsFragment_self"
            app:destination="@id/movieDetailsFragment"
            app:enterAnim="@anim/enter_from_right"
            app:exitAnim="@anim/exit_to_right" />
    </fragment>
    <fragment
        android:id="@+id/saved_movies"
        android:name="com.example.movies.ui.saved_movie.SavedMovieFragment"
        android:label="Watch list"
        tools:layout="@layout/fragment_saved_movie">
        <action
            android:id="@+id/action_saved_movies_to_movieDetailsFragment"
            app:destination="@id/movieDetailsFragment"
            app:enterAnim="@anim/enter_from_right"
            app:exitAnim="@anim/exit_to_right" />
    </fragment>
    <fragment
        android:id="@+id/nav_login_fragment"
        android:name="com.example.movies.ui.login.LoginFragment"
        android:label="fragment_login"
        tools:layout="@layout/fragment_login">
        <action
            android:id="@+id/action_nav_login_fragment_to_homeFragment"
            app:destination="@id/moviesFragment"
            app:popUpTo="@id/nav_login_fragment"
            app:popUpToInclusive="true"/>
    </fragment>
    <fragment
        android:id="@+id/settings"
        android:name="com.example.movies.ui.settings.SettingsFragment"
        android:label="Settings">
        <action
            android:id="@+id/action_settings_to_nav_login_fragment"
            app:destination="@id/nav_login_fragment"
            app:popUpTo="@id/moviesFragment"
            app:popUpToInclusive="true"/>
    </fragment>
    <fragment
        android:id="@+id/welcome_screen"
        android:name="com.example.movies.ui.welcome_screen.WelcomeScreenFragment"
        android:label="fragment_welcome_screen"
        tools:layout="@layout/fragment_welcome_screen">
        <action
            android:id="@+id/action_welcomeScreenFragment_to_nav_login_fragment"
            app:destination="@id/nav_login_fragment"
            app:popUpTo="@id/welcome_screen"
            app:popUpToInclusive="true" />
        <action
            android:id="@+id/action_welcome_screen_to_homeFragment"
            app:destination="@id/homeFragment"
            app:popUpTo="@id/welcome_screen"
            app:popUpToInclusive="true"/>
    </fragment>
    <fragment
        android:id="@+id/moviesFilterFragment"
        android:name="com.example.movies.ui.movie_filter.MoviesFilterFragment"
        tools:layout="@layout/fragment_movies_filter">
        <action
            android:id="@+id/action_moviesFilterFragment_to_homeFragment"
            app:destination="@id/moviesFragment"
            app:popUpTo="@id/moviesFragment"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@+id/homeFragment"
        android:name="com.example.movies.ui.home.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" >
        <action
            android:id="@+id/action_homeFragment_to_moviesFragment"
            app:destination="@id/moviesFragment" />
        <action
            android:id="@+id/action_homeFragment_to_movieDetailsFragment"
            app:destination="@id/movieDetailsFragment"
            app:enterAnim="@anim/enter_from_right"
            app:exitAnim="@anim/exit_to_right" />
    </fragment>
    <fragment
        android:id="@+id/explore_fragment"
        android:name="com.example.movies.ui.explore.ExploreFragment"
        android:label="Explore"
        tools:layout="@layout/fragment_explore">
        <action
            android:id="@+id/action_exploreFragment_to_moviesFragment"
            app:destination="@id/moviesFragment" />
    </fragment>


</navigation>

This is XML of ExploreFragment()


<androidx.constraintlayout.widget.ConstraintLayout 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=".ui.explore.ExploreFragment">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/home_page_toolbar_color"
        android:theme="@style/ToolBarStyle"
        android:elevation="4dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.cardview.widget.CardView
        android:id="@+id/card_view_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        app:cardCornerRadius="32dp"
        app:cardElevation="12dp"
        app:layout_constraintTop_toBottomOf="@id/toolbar">

        <androidx.appcompat.widget.SearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="34dp"
            android:background="@drawable/background_search_view"
            app:iconifiedByDefault="false"
            app:queryHint="@string/search_view_hint"
            tools:ignore="RtlSymmetry" />
    </androidx.cardview.widget.CardView>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment_container_explore"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="15dp"
        android:name="com.example.movies.ui.movie_categories.MovieCategoriesFragment"
        app:layout_constraintTop_toBottomOf="@id/card_view_search"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
0 Answers
Related