0

App Structure: My Main Activity is also acting as a Search Activity with a SearchView in the ActionBar. When I search in the ActionBar I handle it with onNewIntent in the Main Activity. When the intent is ACTION_SEARCH I replace the current fragment in MainActivity (possibly nothing or a previous search fragment) with a new search fragment which then creates an ASyncTask to fetch some data from an API which then creates another ASyncTask to get more details about the data and then populates a list view in the fragment.

Problem: after performing a search the SearchView field is empty and focused again.

Goal: I want the SearchView to not be focused and the SearchText to stay in case the user wants to change it.

What I have succeeded with is closing the action view (and removing focus) with MenuItemCompat.collapseActionView()

I've tried clearFocus() and setQuery() and several other methods I've found by googling but nothing seems to work. I've also tried using both in the onQueryTextSumbit function onQueryTextListener without success. I've looked at how other Apps like F-Droid and Simpletask Cloudless achieve it and most of them use filters on already existing data, whereas I am using ASyncTasks, which I think could be a source of the problem.

jmply
  • 41
  • 6
  • 1
    you should use `SearchView.OnQueryTextListener` - it works with no problems – pskink Mar 29 '17 at 19:00
  • I've tried several solutions with OnQueryTextListener, none of them worked for me. – jmply Mar 29 '17 at 19:06
  • 1
    i used it many, many times, it works quite good especially with a `CursorAdapter` + `FilterQueryProvider`, just call `adapter.getFilter().filter()` – pskink Mar 29 '17 at 19:07
  • The problem is I'm creating an Asynctask to fetch data from an API based on the search. I cannot filter all of the JSON objects the API can deliver nor can I download all of them and filter them. – jmply Mar 29 '17 at 19:49
  • 1
    it has nothing to do with downloading all of data and filtering it, run [this](http://stackoverflow.com/a/19860624/2252830) simple code and you will see how it works - it uses `adapter.getFilter().filter()` internally but `FilterQueryProvider` is called each time you want to search for some text - see `runQuery` method – pskink Mar 29 '17 at 19:52
  • Okay this helps, I tried it out and see what you mean. I'll try to apply it to my problem tomorrow. Thank you. – jmply Mar 29 '17 at 20:02
  • 1
    and no, you don't need any AsyncTask for that since runQuery method is called in a background thread – pskink Mar 29 '17 at 20:05

1 Answers1

0

So I found an alternative solution to what was proposed in the comments (Thank you regardless pskink). When my MainActivity received the SearchIntent it created a new search list Fragment (this was my mistake). I changed the search fragment creation to OnCreate in my MainActivity with a separate function to populate the already created fragment and now clearFocus() properly works in the onQueryTextSubmit function of my SearchView listener.

jmply
  • 41
  • 6