'Android studio, how to navigate to a fragment keeping the same selected menu item

I have a profile based app. I have the bottom navigation bar working perfectly. when you search for a user in the search fragment and click on the user, it navigates to the profile fragment. However, I want the bottom navigation to stay on the search menu item label (similar to instagram). How would I go about this? Here is my 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/navigation_profile">

    <fragment
        android:id="@+id/navigation_map"
        android:name="com.example.myapplication.fragments.map.MapFragment"
        android:label="@string/title_map"
        tools:layout="@layout/fragment_map" />

    <fragment
        android:id="@+id/navigation_search"
        android:name="com.example.myapplication.fragments.search.SearchFragment"
        android:label="Search"
        tools:layout="@layout/fragment_search">
        <action
            android:id="@+id/action_navigation_search_to_navigation_profile"
            app:destination="@id/navigation_profile" >
            <argument
                android:name="isAdmin"
                app:argType="boolean" />
            <argument
                android:name="Username"
                app:argType="string" />
            <argument
                android:name="from"
                app:argType="string" />
        </action>
    </fragment>

    <fragment
        android:id="@+id/navigation_camera"
        android:name="com.example.myapplication.fragments.camera.CameraFragment"
        android:label="@string/title_camera"
        tools:layout="@layout/fragment_camera">
        <action
            android:id="@+id/action_navigation_camera_to_navigation_profile"
            app:destination="@id/navigation_profile" >
            <argument
                android:name="isAdmin"
                app:argType="boolean" />
            <argument
                android:name="Username"
                app:argType="string" />
            <argument
                android:name="mainProfile"
                app:argType="string" />
            <argument
                android:name="from"
                app:argType="string" />
        </action>
    </fragment>

    <fragment
        android:id="@+id/navigation_leaderboard"
        android:name="com.example.myapplication.fragments.leaderboard.LeaderboardFragment"
        android:label="@string/title_leaderboard"
        tools:layout="@layout/fragment_leaderboard" >
        <action
            android:id="@+id/action_navigation_leaderboard_to_navigation_profile"
            app:destination="@id/navigation_profile"
            app:launchSingleTop="true"
            app:popUpTo="@id/navigation_profile"
            app:popUpToInclusive="true">
            <argument
                android:name="isAdmin"
                app:argType="boolean" />
            <argument
                android:name="Username"
                app:argType="string" />
            <argument
                android:name="from"
                app:argType="string" />
        </action>
    </fragment>

    <fragment
        android:id="@+id/navigation_profile"
        android:name="com.example.myapplication.fragments.profile.ProfileFragment"
        android:label="@string/title_profile"
        tools:layout="@layout/fragment_profile" >
        <action
            android:id="@+id/action_navigation_profile_to_navigation_post"
            app:destination="@id/navigation_post" >
            <argument
                android:name="argQR"
                app:argType="string" />
            <argument
                android:name="argPostUser"
                app:argType="string" />
            <argument
                android:name="argUser"
                app:argType="string" />
            <argument
                android:name="argAdmin"
                app:argType="boolean" />
        </action>
    </fragment>

    <fragment
        android:id="@+id/navigation_post"
        android:name="com.example.myapplication.fragments.post.PostFragment"
        tools:layout="@layout/fragment_post"
        />

</navigation>

I know I could make navigating to the profile into a global navigation. I just want to get this tab selected item working first.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source