Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Worker thread will update an EditText in the UI thread

  1. #1
    Junior Member
    Join Date
    Feb 2023
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Worker thread will update an EditText in the UI thread

    In my Android/Google Play 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?
    I tried TextChangedListener in the UI together with runOnUiThread in the worker thread but I could not make that 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, but I am feeling more like confused

    It seems to me that what I want to do is quite common and at least conceptually simple, 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

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,049
    Thanks
    63
    Thanked 2,710 Times in 2,660 Posts

    Default Re: Worker thread will update an EditText in the UI thread

    My Android is a bit rusty. Try asking your question on this forum: https://coderanch.com/f/93/Android
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2023
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Worker thread will update an EditText in the UI thread

    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, when I was exploring the idea.
    Happily, life goes on

Similar Threads

  1. Replies: 1
    Last Post: September 24th, 2013, 04:18 PM
  2. Update GUI from thread
    By robc3129 in forum Object Oriented Programming
    Replies: 2
    Last Post: June 20th, 2013, 02:30 PM
  3. example for updation of JTABLE using swing worker thread
    By harshilshah in forum AWT / Java Swing
    Replies: 2
    Last Post: April 26th, 2013, 11:45 AM
  4. Jtble not updating using swing worker thread ?
    By harshilshah in forum AWT / Java Swing
    Replies: 2
    Last Post: April 26th, 2013, 09:10 AM
  5. Replies: 4
    Last Post: June 15th, 2012, 01:50 PM

Tags for this Thread