0

When I do some processing in the background thread and wants to update the UI using runOnUiThread(), the code inside the function is running successfully without any error, but the UI gets rendered only if the user touches the screen. Otherwise, it's rendering consistently.

Why it's rendering only after touching the screen ?

Ankita Shah
  • 1,866
  • 16
  • 31
Kamalakannan J
  • 2,818
  • 3
  • 23
  • 51

3 Answers3

2

It is possible if your screen gets overlapped by another screen. This causing Activity to move to paused state (.onPause()) method. when you touch it again, it become foreground again so can receive UI update events

Alex Shutov
  • 3,217
  • 2
  • 13
  • 11
2

It's possible that an operation is blocking (for example notifying an adapter too often) your ui thread and gets unblocked with an interrupt occurs. Touching the screen is an interrupt in your scenario.

If you paste your code maybe we can find an exact solution.

Edit: Debounce example code with RxJava

          yourSearchObservable
            .throttleLast(100, TimeUnit.MILLISECONDS)
            .debounce(200, TimeUnit.MILLISECONDS)
            .onBackpressureLatest()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    searchResults -> {
                        // Your success

                   } ,
                    () -> {
                        // Error
                    }
            ));
savepopulation
  • 11,736
  • 4
  • 55
  • 80
  • I think, this is the reason for it because it happens in this way. 1. User types in the search input field. 2. Query string is passed to background thread for DB querying. 3. After DB querying, I'm setting the data on adapter in the runOnUiThread(). Since I'm not Debouncing the searching input, after each character press, the above process is happening. What is the best way to implement search in local DB and update on list view. – Kamalakannan J Nov 24 '16 at 17:33
  • you can debouce user inputs. for example you can query your db after 1 second and notify your list. by the way can you accept my answer? thanks. – savepopulation Nov 24 '16 at 18:17
  • Can you post the code to debounce user input to search ? Because I couldn't able to find a valid working code.. – Kamalakannan J Nov 24 '16 at 23:28
1

Try using callbacks or android.os.Handler.post() to interact with the Main Thread