material-components / material-components/material-components-android
[Search Bar] Query text stays in search bar and search view despite fragment change and explicit setText("")
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 17.4k
- Forks
- 3.2k
- PR merge metrics
- No merged PRs in 30d
Description
**Description:** I am using similar setup you describe in the [documentation](https://github.com/material-components/material-components-android/blob/master/docs/components/Search.md#putting-it-all-together) 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
```
```
**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](https://github.com/material-components/material-components-android/releases) of the library.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the SearchBar and SearchView setup in the issue and compare it with the documented setup and SearchRecyclerDemoActivity. Reproduce switching fragments through bottom navigation, then inspect how the views are recreated and retain their query text. Done means the search bar and search view are empty when returning to the fragment.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100