• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Worker thread will update an EditText in the UI thread

In my app I need to call a worker thread which will retrieve technical information from the internet,
update either an EditText or a global variable in the UI thread, and then return to the UI thread where
the updated field will be used in further processing. I have a small experience with Threads having used ASYNCTASK and a simple thread but I am having difficulty getting into this one.

My understanding so far is that, on the one hand "this cannot be done", but on the other hand it can be done by choosing an appropriate synchronization technique. Pretty much all of the examples I have seen do not need the UI to use the returned result, at least not immediately, so I have not recognized an appropriate technique in my searching. I have tried several techniques for passing data between main thread and worker threads.
Examples:
I tried runOnUiThread to set the final value, but It seems to run on the UI thread after the UI thread has carried on?
TextChangedListener in the UI together with runOnUiThread in the worker thread but that does not work either?
I have read about Synchronization but this seems wrong because the main thread must be blocked?
I started looking at WorkManager, RxJava, Eventbus, BroadcastReceiver and more.

It seems to me that what I want to do is quite common and that someone should be able to guide me on which technique might be best to use with perhaps a simple example. I do not mind doing the hard work of learning new stuff but I would really appreciate a starting point for this one.

Thank you, hope you can help :)
Mick
 
When I get stuck in this stuff is I like to spend some time writing a question, like above, and I leave it until the next day before posting. Last night I was tired and just hit the post button. And as usual, trying unsuccessfully to get to sleep, it occurred to me what was the problem/solution.
"this cannot be done" is correct in this circumstance...
I am running 3 loops. 2 of which access the internet and require a background thread which returns a result. The solution, at least in this case, is to take out the loops, put a couple of buttons on the UI, and let the user activate the process directly. The result can then be simply deposited into a TextBox or a global variable using runOnUiThread. I know this works because it did in the test case, simple subset program of my larger App code.
Happily, life goes on :)
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones