0

I have 3 different fragments in the same activity, and the Activity contains a toolbar with a settings button. When I press the settings button, the Settings Fragment should open. How can I implement this using Navigation? Should I create an action from every single fragment to Settings fragment or there is a simple way? (considering that the settings button is in activity layout). I also created a diagram for better understanding. If you have any questions let me know. Thanks

This is how I set the navigation graph in my activity:

private fun setDefaultNavGraph(bundle: Bundle?){
    fragmentContainer = my_nav_host_fragment as NavHostFragment
    val inflater = fragmentContainer.findNavController().navInflater
    val graph = inflater.inflate(R.navigation.prb_navigation)
    fragmentContainer.findNavController().setGraph(graph, bundle)
}

And this is my activity xml file:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.eschbachit.citylinemobile.activities.PrbModuleActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolbar_height"
        android:background="@color/toolbarDarkGrey"
        app:theme="@style/ToolbarColoredWhiteIcon"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        app:contentInsetRight="0dp"
        app:contentInsetEnd="0dp">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/back_btn"
            android:layout_width="24dp"
            android:layout_height="match_parent"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
            android:layout_marginStart="4dp"
            app:srcCompat="@drawable/back_button_icon"
            android:tint="@color/white"
            android:visibility="gone"
            android:contentDescription="@null"/>

    <TextView
        android:id="@+id/prb_activity_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:ellipsize="end"
        android:text="@string/title_activity_main"
        android:textSize="@dimen/font_size_default"
        android:lineSpacingExtra="4sp"
        android:textStyle="bold"
        android:textColor="@color/white"
        android:layout_gravity="center"/>

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/settings_nav_tv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="@color/white"
        app:srcCompat="@drawable/ic_settings"
        android:layout_gravity="end"
        android:textSize="@dimen/font_size_default"
        android:paddingTop="12dp"
        android:paddingBottom="12dp"
        android:gravity="center" />

    </androidx.appcompat.widget.Toolbar>

    <fragment
        android:id="@+id/my_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        android:layout_below="@+id/toolbar"/>

</RelativeLayout>

enter image description here

Razvan22
  • 153
  • 2
  • 12

3 Answers3

0

Expecting all your fragments lie on a single activity, you can do it in following ways :-

  1. Adding action (as you said)

Navigation.findNavController(...).navigate(destination.id);

  1. You can also popBackStack if the parent of Fragment A,B,C is Settings Fragment.

    Navigation.findNavController(...).popBackStack();

Kaveri
  • 1,060
  • 2
  • 12
  • 21
0

SOLUTION:

The thing I was looking for was a Generic action in my nav_graph. I found the solution here.

I added this action to my navigation file and I used it in my activity in setOnClickListener method.

<action android:id="@+id/openOnlineSettingsFragment"
    app:destination="@id/onlineSettingsFragment"
    app:enterAnim="@anim/pull_in_right"
    app:exitAnim="@anim/push_out_left"
    app:popEnterAnim="@anim/pull_in_left"
    app:popExitAnim="@anim/push_out_right" />
Dharman
  • 30,962
  • 25
  • 85
  • 135
Razvan22
  • 153
  • 2
  • 12
0

Create two graph one main_graph and another fragments_graph. Add settingFragment to main_graph and also include fragments_graph to main_graph as follow:

<?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/main_graph"
    app:startDestination="@+id/settingFragment">

    <fragment
        android:id="@+id/settingFragment"
... />
    <action
        android:id="@+id/goToSetting"
        app:destination="@id/settingFragment" />

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

</navigation>

Now write a method that its action is the following and this will do what you want: (put it in parent activity)

findNavController().navigate(MainGraphDirection.goToSetting())
C.F.G
  • 817
  • 8
  • 16