1

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. enter image description here

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>
Miguel Manzo
  • 171
  • 9
  • If you're using the Jetpack Navigation Component and the NavHostFragment, then you're never supposed to replace the NavHostFragment with a different fragment. Use the Jetpack Navigation API, or don't use it at all. Not inbetween. – EpicPandaForce Jul 19 '20 at 17:36
  • Understood. I just want to move to the other fragment when clicking the 'button_cart_button', which will be visible among several fragments, while saving the current fragment to the backstack and without messing up the navigation component logic. – Miguel Manzo Jul 19 '20 at 18:00
  • 1
    Found the answer: https://stackoverflow.com/questions/51085222/navigate-to-fragment-on-fab-click-navigation-architecture-components – Miguel Manzo Jul 19 '20 at 23:37

0 Answers0