Android боковое меню. Как с этим работать?

Есть шаблонный проект сгенерированный студией с боковой навигацией DrawerLayout. Например, вторая вкладка содержит список и по нажатию нужно открывать детальное описание выбранного элемента. Сейчас я сделал по нажатию на элемент supportFragmentManager.beginTransaction().replace(container, fragment).addBackStack(null).commit()

Но тогда меняется логика стека и обратное нажатие некорректно выбрасывает. Даже если через обратный вызов делать транзакцию из хозяйской Activity.

Из костылей придумал:

  1. Сделать внутри фрагмента отвечающего за список свою навигацию и двигать логику там, но выглядит как-то избыточно
  2. Pager - совсем страшно

Какими средствами можно организовать переход внутри отдельного пункта из бокового меню чтобы работал корректно стек?


Ответы (1 шт):

Автор решения: Android Junior

Сложность была в формулировке вопроса, но я чудом нашел в документации возможность использовать навигацию как элемент навигации. Надеюсь это кому-то поможет в будущем: https://developer.android.com/guide/navigation/navigation-nested-graphs

Как итог мои разметки стали такие:

main_activity_drawer

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_student"
            android:icon="@drawable/ic_menu_camera"
            android:title="@string/student_section" />
        <item
            android:id="@+id/nav_portfolio"
            android:icon="@drawable/ic_menu_gallery"
            android:title="@string/portfolio_section" />
        <item
            android:id="@+id/nav_messenger"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="@string/messenger_section" />
    </group>
</menu>

основная навигация с include

<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/nav_student">

    <fragment
        android:id="@+id/nav_student"
        android:name="com.chistoedet.android.istustudents.ui.main.studentOffice.StudentOfficeFragment"
        android:label="@string/student_section"
        tools:layout="@layout/fragment_student" />

    <fragment
        android:id="@+id/nav_portfolio"
        android:name="com.chistoedet.android.istustudents.ui.main.portfolio.PortfolioFragment"
        android:label="@string/portfolio_section"
        tools:layout="@layout/fragment_gallery" />


    <include
        app:graph="@navigation/messenger_navigation"
        />

   <!-- <fragment
        android:id="@+id/nav_messenger"
        android:name="com.chistoedet.android.istustudents.ui.main.messenger.chat.ChatFragment"
        android:label="@string/messenger_section"
        tools:layout="@layout/chat_fragment" />-->

    <fragment
        android:id="@+id/nav_profile"
        android:name="com.chistoedet.android.istustudents.ui.main.profile.ProfileFragment"
        android:label="@string/messenger_section"
        tools:layout="@layout/profile_fragment" />

</navigation>

И реализация навигации внутри пункта nav_messenger

<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/nav_contact_list"
    android:id="@+id/nav_messenger">

    <fragment
        android:id="@+id/nav_contact_list"
        android:name="com.chistoedet.android.istustudents.ui.main.messenger.list.ContactListFragment"
        android:label="Контакты"
        tools:layout="@layout/fragment_contact_list" >
        <action
            android:id="@+id/action_nav_contact_list_to_nav_chat"
            app:destination="@id/nav_chat" />
    </fragment>

    <fragment
        android:id="@+id/nav_chat"
        android:name="com.chistoedet.android.istustudents.ui.main.messenger.chat.ChatFragment"
        tools:layout="@layout/chat_fragment" >
        <action
            android:id="@+id/action_nav_chat_to_nav_contact_list"
            app:destination="@id/nav_contact_list" />
    </fragment>

</navigation>
→ Ссылка