back button does not work well when using search view

Viewed 33

I'm new in android and I have a search view in a fragment that when its text changes the results appear in a recycler view. The problem is when back button is pressed, only the query text is removed and it does not go to the previous fragment. What shall I do to make the back button work as it should.

<?xml version="1.0" encoding="utf-8"?>
       <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"
        android:layout_margin="8dp">
    
         <androidx.appcompat.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_marginTop="16dp"
            android:layoutDirection="rtl"
            app:iconifiedByDefault="false"
            app:layout_constraintTop_toTopOf="parent"
            
            app:searchIcon="@drawable/ic_baseline_search_24" />
    
         <androidx.recyclerview.widget.RecyclerView
             android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:layoutDirection="rtl"
            android:orientation="horizontal"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintTop_toBottomOf="@id/searchView" />
        </androidx.constraintlayout.widget.ConstraintLayout>

.

<?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"
    android:id="@+id/shopping_nav"
    app:startDestination="@id/homeFragment">

      <fragment
            android:id="@+id/homeFragment"
            android:name="com.example.shopingofmine.ui.home.HomeFragment"
            android:label="Home" >
          >
            <action
                android:id="@+id/action_homeFragment_to_searchFragment"
                app:destination="@id/searchFragment"
                 />
           
        </fragment>
        <fragment
            android:id="@+id/searchFragment"
            android:name="com.example.shopingofmine.ui.search.SearchFragment"
            android:label="SearchFragment" >
         
 
           </fragment>
     </navigation>
0 Answers
Related