I want to show a Toast right after the user clicks on a CheckBoxPreference in my PreferenceActivity.
myCheckBox.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Toast.makeText(Prefs.this,
"test",
Toast.LENGTH_SHORT).show();
doSomething();
return false;
}
});
I also tried to put the Toast into the doSomething() method, but it’s always shown after the whole method is processed. I tried getBaseContext() instead of Prefs.this, but it didn’t help.
Any idea why the Toast doesn’t show up at once and how to make it do so?
This is happening because the
onPreferenceClicklistener is running in the UI thread. This thread is also the same as the one that handles displaying theToast.Toast#showonly pushes a message onto the message queue that will then run code to make theToastdisplay. That queue won’t be processed until after youronPreferenceClickhandler is completely finished.You can try:
This will cause the
Toastto post to the message queue then yourdoSomethingwill also be posted to the queue after the toast. The downside to this is that there could be UI messages that will be handled beforedoSomethingis called. Also, ifdoSomethingis long running it will monopolize your UI thread and could cause a possible ANR force close. You may want to think about runningdoSomethingin anAsyncTaskif it takes more than 150ms or so.