I want to be able to open my CartFragment through a Button OnClickLister set in the parent Activity (All fragments have the same parent Activity and the navGraph is connected and working with all fragments in the navigation component XML file) but I don't know how while using Navigation Component. I have several hours researching with no avail.
My intent is to navigate to the other fragment while using navigation component but I don't know how to create the action to move between the activity to the fragment.
Here is my MainActivity layout:
<layout 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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- android:paddingTop="?attr/actionBarSize"-->
<com.google.android.material.button.MaterialButton
android:id="@+id/button_cart_button"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:drawableLeft="@drawable/ic_smart_cart_new_icon"
android:text="Ir al carrito"
android:textSize="16sp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.001"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
</layout>