material-components / material-components/material-components-android
[Search Bar] Query text stays in search bar and search view despite fragment change and explicit setText("")
Nessuno ha ancora preso questa issue.
- Lingua principale
- Java
- Stelle
- 17.4k
- Fork
- 3.2k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
Description: I am using similar setup you describe in the documentation and SearchRecyclerDemoActivity.
I have a search bar and a recyclerview below it.
Once I click on a search bar, search view pops up and I enter a search query, press ok and search view closes, search bar is populated with a query text and recycler view below gets updated.
When I switch to other fragments in my bottom navigation bar and then later come back to the search fragment, recycler view is re-initialized with the full list of items, but the old query is still inside the search bar and search view.
Explicitly setting searchBar.setText(""); searchView.setText(""); searchView.getEditText().setText(""); inside fragment.onCreateView() does not help, same with fragment.onDestroyView().
In a debugger I can see that once I come back to the search fragment, new instances of searchBar and searchView are returned from binding.getRoot().findViewById(...).
Expected behavior: Query text in search bar and search view should be cleared once fragment/activity is changed.
Source code: The code snippet which is causing this issue
<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"
tools:context=".ui.library.LibraryFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listitem="@layout/recycler_view_item" />
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.search.SearchBar
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search" />
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.search.SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Search"
app:layout_anchor="@id/search_bar">
</com.google.android.material.search.SearchView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Android API version: 33
Material Library version: 1.9.0-rc01
Device: xiaomi mi note 10 lite (MIUI Global 12.5.6 Stable)
To help us triage faster, please check to make sure you are using the latest version of the library.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la configurazione di SearchBar e SearchView nell’issue e confrontala con la configurazione documentata e con SearchRecyclerDemoActivity. Riproduci il passaggio tra i fragment tramite la navigazione inferiore, quindi esamina come le view vengono ricreate e mantengono il testo della query. Il lavoro è completato quando search bar e search view sono vuoti tornando al fragment.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- android
- Ambito
- mobile
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100