Android: Runonuithread Does Not Always Choose The Right Thread?
I've got an activity that keeps reading words to the user, and using onUtteranceCompleted with textTospeech to display something when the code is completed. Inside onUtteranceCompl
Solution 1:
I would use a Handler instead of runOnUiThread().
For one thing, you're using a Thread that starts another Thread - why?
Secondly, if you create a simple Handler, it should kill itself on the rotate config change. IE:
privateHandlerhandler=newHandler() {
@OverridepublicvoidhandleMessage(Message msg) {
// do your background or UI stuff
}
};
Then later use a Thread to call the handler, which will kick off whatever process you want to run on the UI thread:
new Thread() {
@Override
publicvoidrun() {
long timestamp = System.currentTimeMillis();
// thread blocks for your 1 second delaywhile (System.currentTimeMillis() - timestamp <= 1000) {
// loop
}
handler.sendEmptyMessage(0);
}
}.start();
Solution 2:
Ok so this is a fix I've come up with, if someone has a better solution, I'm listening.
- I've added
android:configChanges="keyboardHidden|orientation"
inside the activity in the androidmanifest
2.
and then a function that is called when the screen is rotate:
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setContentView(R.layout.streaming);
initializeUI(); //contains all the findViewByID etc...
}
Post a Comment for "Android: Runonuithread Does Not Always Choose The Right Thread?"