androidkotlinandroidxnavgraphnavcontroller

How to skip SearchView in back stack of navigation controller?


The call docPageSearchView.show() opens SearchView. Then, pick an item from the search result and open a new instance of the same fragment to display the new content ("viewer fragment"). Now, press the back button. SearchView opens again. How do I get back to the previous instance of the viewer fragment? In other words, how do I skip SearchView?

override fun onMenuItemClick(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.doc_page_search_menu_item -> binding.docPageSearchView.show()
        else -> return false
    }
    return true
}

override fun onItemClicked(model: PageEntity, position: Int, view: View) {
    binding.docPageSearchView.hide()
    navController.navigate(DocPageViewerFragmentDirections.toSelf(model.key))
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">

    <com.google.android.material.search.SearchView
        android:id="@+id/doc_page_search_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="@string/hint_doc_page_search">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/search_result_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

    </com.google.android.material.search.SearchView>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="?actionBarSize"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:id="@+id/markdown_root"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="@dimen/view_indent_horizontal"
            android:isScrollContainer="true"
            android:scrollbarThumbVertical="@android:color/transparent"
            android:textAppearance="@style/TextAppearance.Material3.BodyLarge"
            android:textColor="?attr/colorOnBackground"
            android:textIsSelectable="true"
            tools:text="Lorem ipsum dolor sit amet" />

    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/doc_page_bottom_app_bar"
        style="@style/Widget.Material3.BottomAppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:hideOnScroll="true"
        app:menu="@menu/menu_navigation_doc_page" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Solution

  • Set visibility to false. When the user returns to the previous screen, the SearchView will be hidden.

    override fun onItemClicked(model: PageEntity, position: Int, view: View) {
        binding.docPageSearchView.setVisible(false)
        binding.docPageSearchView.hide()
        navController.navigate(DocPageViewerFragmentDirections.toSelf(model.key, model.languageId))
    }