У меня есть следующий nav_graph 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"
app:startDestination="@id/users_fragment"
android:id="@+id/nav_graph">
<fragment
android:id="@+id/users_fragment"
android:name="com.package.UsersFragment"
tools:layout="@layout/users_fragment"
android:label="UsersFragment">
<action
android:id="@+id/action_users_fragment_to_profile_fragment"
app:destination="@id/profile_fragment"
app:popUpTo="@id/users_fragment"
app:popUpToInclusive="true"/>
</fragment>
<fragment
android:id="@+id/profile_fragment"
android:name="com.package.ProfileFragment"
android:label="ProfileFragment">
<argument
android:name="userId"
app:argType="string" />
</fragment>
</navigation>
Основным видом деятельности xml является:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.package.UsersViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:background="@color/colorPrimary" />
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/app_bar_layout"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Для перехода к деталям, которые я звоню:
val action = UsersFragmentDirections
.actionUsersFragmentToProfileFragment(
userId = user.userId,
)
navController.navigate(action)
, где UsersFragmentDirections
генерируется плагином safe-args.
Проблема заключается в том, что при повторном нажатии главный фрагмент не отображается: действие закрывается. Если в действии я использую app:popUpToInclusive="false"
, то кнопка назад больше не работает. Что я должен сделать, чтобы иметь возможность go к UsersFragment из ProfileFragment, когда пользователь нажимает назад?