5

I am using jetpack navigation and I need to pass some parameters through activity. With my actual config when I call to findNavController the fragment is called twice.

This is normal because we are calling to fragment programatycally and in the xml with the app:navGraph="@navigation/nav_graph"

can I have one way to have both and ignore the xml? The developers sometimes make mistakes and write it in both placesand no one notices that the fragment is being called twice.

androidx.navigation:navigation-fragment-ktx:2.3.3
androidx.fragment:fragment-ktx:1.3.0
findNavController(findViewById(R.id.nav_host_fragment))
    .setGraph(R.navigation.nav_graph, Bundle())

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <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_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Rulogarcillan
  • 1,130
  • 2
  • 13
  • 23

0 Answers0