I am Using android.support.v7.widget.SearchView
and I am performing Search on enter of every character in SearchView. following is the snippet.
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextSubmit(String query)
{
return false;
}
@Override
public boolean onQueryTextChange(String newText)
{
if (!newText.trim().equals(""))
{
displayResult(newText);
return false;
}
}
});
And I am updating the UI When I am Getting Result, So In My Case It will call Search API every time when user enter a single character. So Suppose user wants to Search "Australia" and try to enter whole word but My UI is Updating with Search and User has just type "Aus" after the Keyboard is not responding because it's updating the UI.
So I want Just want to respond Keyboard while Updating the UI.
Hope I'll get fast response.
The scenario of your case is:
Input text
Perform search
Display results
Do again
The lagging happened because of (2) + (3) cost much time to do.
Try to send the search key from (1) to worker thread (AsyncTask, Thread, IntentService,...).
(2) will be execute on worker thread then push the results back to UI
(3) update the UI
To improve performance, I think you should perform search while user stop the input at period of time. In additional, stop unneccessary search if the input changed.
example: input1 > search1 > input2 > cancel old task1 > search2 > ...
Hope it help!
The key to the problem is both updating UI operation with the result from Search API and responding to SearchView with Keyboard are do its job in Main(UI) Thread.
In a short, you let the main thread do too much thing in a short time.
So the solution is to try to LIMIT the frequency of updating UI operation. I meaning, you should let the displayResult(newText)
method can CANCEL the previous call when the user continuously inputted word in a short time.
Just updating the UI with the result ONCE when the whole word Australia
was inputted completely.
BTW, your search API call should do in the background thread with some wonderful library(Retrofit && OkHttp).
There might be two things that you might be doing wrong. 1) Network request are performed from UI Thread 2) Parsing is done on UI Thread Judging from your stackoverflow points you might not be doing the first case ie; performing a network request on the UI Thread.
For Case 2: try to perform parsing on a background thread, try to shorten the json response (tell the author of the Search API to give a lightweight response). And for each query change cancel the previous task
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With