Android UI thread

I wanted to query a database on the onCreate method, but I ran into issues when doing this from another thread.

First I tried to do it with the View.post() method, but it seems like the View is not always attached to the window, so the run() method of the Runnable was never called.

Another options was to create a Handler() member and call the run() method on that. That did work.

Another option and the one I went for is to call runOnUiThread() with the Runnable. Because I didn't have to create a new member variable like Handler() I chose this option.

I still don't understand why the first option sometimes (mostly when I stopped and started the application again) did not always work. I thought after setContentView() the views are attached to the window.

Tag: | Category: