Android, updating the UI from another thread
So you can create a background thread for an Android app and happily do your thing. But now you want to update the UI from that thread you've just created and something's causing your app to crash. Your problems most likely solvable by using this simple solution... (This post assumes you know how to make threads, if not then read my last post)
A method called runOnUiThread can be used for the section of code that updates your UI and can be called from wherever you like. Check the solution below:
// This is the thread that works in the background backgroundUpdate = new Thread(new Runnable() { public void run() { // Do your thing here... // Do this because only the UI thread can update the UI elements. runOnUiThread(new Runnable() { public void run() { // Put your UI update code here TextView titleText = (TextView) findViewById(R.id.title_text); titleText.setText(page.getTitle()); TextView summaryText = (TextView) findViewById(R.id.summary_text); summaryText.setText(page.getSummary()); } }); } });
Published at 4 Nov 2012, 22:56 PM
Tags: Android