Android Multiple Nav Host in Navigation Component

Я использую single Activity pater в своем проекте. В одном из фрагментов, назовем его ExploreFragment(), мне нужно создать FragmentContainerView, в котором будут меняться несколько фрагментов. ExploreFragment() имеет поисковую систему и по умолчанию отображает MovieGanresFragment() со списком жанров. Если я начинаю вводить текст в поисковик, фрагмент меняется на MovieByNameFragment(), где отображается список фильмов с названием, введенным пользователем. Вопрос в том, как мне реализовать ExploreFragment(), где будет контейнер с несколькими фрагментами в случае когда ExploreFragment() находится в основном графе Navigation component?

Я решил создать ExploreFragment() в основном графе и создать в нем FragmentContainerView для переключения двух фрагментов MovieGanresFragment() и MovieByNameFragment() . Для переключения фрагментов в ExploreFragment() я использую 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()
    }

это работает, но возник другой вопрос. Как перейти от фрагмента MovieByNameFragment(), которого нет в основном графе, к фрагменту MoieDetailsFragment(), который находится в основном графе, и передать туда данные?

Это XML моего Navigation component:

<?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>

Это XML фрагмента ExploreFragment()

<?xml version="1.0" encoding="utf-8"?>
<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 шт):