Using Variables on UI Thread from Worker Thread
Asked Answered
P

4

3

The Android Developers site states that two rules must be followed when working in the UI thread, i.e.,

  1. Do not block the UI thread

  2. Do not access the Android UI toolkit from outside the UI thread*

Does this mean that I can access variables in the UI thread from within a worker thread, i.e., not UI toolkit?

If so, do any special considerations need to be given if the variable is being continually updated, e.g., from a SensorEventListener. Thanks.

Precancel answered 9/9, 2013 at 15:58 Comment(0)
P
2

Does this mean that I can access variables in the UI thread from within a worker thread, i.e., not UI toolkit?

Yes, as long as they are declared as member variables then you can access them. You can even access values in UI elements such as using getText() on a TextView you just can't update any of the UI elements.

do any special considerations need to be given if the variable is being continually updated,

If they are being updated then you may want to have a way of syncing the variables. A good way to do this would be to use an AsyncTask and update the variable in onPostExecute().

If you aren't familiar with using AsyncTask, make sure you look through the Docs several times and understand them.

Pending answered 9/9, 2013 at 16:4 Comment(3)
Thanks. I actually have a sensor listener that's updating a member variable. I then want to have a separate thread that just polls that variable for its value at a set interval. Therefore, if I undersatnd you correctly, I should be able to access that variable directly from within that runnable?Precancel
Do I have to worry about synchronizing in this case, i.e., when the UI thread is writing to the variable, and when the runnable is reading the variable?Precancel
You may need to if there's a chance the two will be changing the value at the same time but I'm not sure you'll run into that in this case but maybe.Pending
F
0

No, you can't access them outside the UI thread. Very few UI elements can be accessed from a non-UI thread with one being ProgressBar.

Freudberg answered 9/9, 2013 at 16:1 Comment(0)
P
0

You can access UI elements in a separate thread but you cannot update them

The only way you can update a UI element in a non-UI thread would be to have a callback to the UI thread in the separate thread using runOnUiThread or using a Handler aside from that you cannot make changes to a UI element in a separate thread

Protractile answered 9/9, 2013 at 16:4 Comment(0)
A
-1

Variables are not to be accessed outside the UiThread. If you'd like to make modifications from outside the UiThread use :

  • Activity.runOnUiThread(Runnable)

  • Some "post" method that stores modifications waiting for the UiThread to treat them. You can use a BlockingQueue for example. Have a look at java.util.concurrent package

  • In some very rare cases the modification of a variable outside the UiThread cannot produce errors in which case it is safe to access it out of the UiThread. In other cases variables should be private, and attempts to access them outside the UiThread should raise IllegalStateException or something like that.

Abatement answered 9/9, 2013 at 16:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.