Maciej Rurarz
Maciej Rurarz

Reputation: 3

New to Android: no screen update via runnable thread or no screen at all. Need some hints

I have dificulties with my app. I'm stuck and have no idea how to resolve my issue which is either no update of buton graphic on receive of UDP data (done by runnable thread) or no graphic at all - just empty screen. Application is avaliable here and I believe that it will be easiest way to review it.

https://github.com/psedlos/android_base_app commit 21 would be most accurate https://github.com/psedlos/android_base_app/commit/d451c9ae4480bcfcbf3b951e2e6d87eec376b162

How I see this application:

There is main data container MainContainer in separate class which suppose to hold whole data so it will simplify accessing it. It also has function to change button graphic based on received data.

In the very beginning I assign all the buttons to it, populate a bit with some data.

Then application create all onClickListeners and then it starts runnable action of listen UDPs. When it comes it analyze data, update Main Container and request it to update graphic.

And here's the problem. I can't access view hierrachy from non-main thread. I tried all the possible way of runOnUIthread but with no success. I created separate thread to call self-update. No success. I have no idea, how could I resolve it.

I tried also invalidate() but once again I had no success at all. Could I ask you for some base hints how should I do it? What is actually best way to create such kind of application? Is my idea of this application wrong from the very beginning?

Upvotes: -3

Views: 59

Answers (1)

Maciej Rurarz
Maciej Rurarz

Reputation: 3

I found very useful way to force UI to update itself when new data income (in my case via UDP). Bit of code which is inside MainContainer class and manipulate Views need to be stored inside Handler:

new Handler(Looper.getMainLooper()).post(new Runnable() {
    @Override
    public void run(){
        [code that manipulate views]
    }
});

Upvotes: 0

Related Questions