Skip to content

Instantly share code, notes, and snippets.

@daithiocrualaoich
Created July 22, 2010 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daithiocrualaoich/486524 to your computer and use it in GitHub Desktop.
Save daithiocrualaoich/486524 to your computer and use it in GitHub Desktop.
trait InMainThread {
private val handler = new Handler
def mainThread(block: => Unit) {
handler.post(new Runnable {
def run { block }
})
}
}
class MyGUIView extends Activity with InMainThread {
private var view: TextView = _
override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
view = new TextView(this)
setContentView(view)
}
def methodCalledByBackgroundThread() {
// Can't invoke view.setText directly because not on the GUI thread, instead...
mainThread { view.setText("Background thread did this.") }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment