views:

50

answers:

1

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?

+1  A: 

This is happening because the onPreferenceClick listener is running in the UI thread. This thread is also the same as the one that handles displaying the Toast. Toast#show only pushes a message onto the message queue that will then run code to make the Toast display. That queue won't be processed until after your onPreferenceClick handler is completely finished.

You can try:

myCheckBox.setOnPreferenceClickListener(new OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Toast.makeText(Prefs.this,
                        "test",
                        Toast.LENGTH_SHORT).show();

                Prefs.this.runOnUiThread(new Runnable() {
                    @Override
                        public void run() {
                            doSomething();
                        }
                    });
                return false;
            }
    });

This will cause the Toast to post to the message queue then your doSomething will also be posted to the queue after the toast. The downside to this is that there could be UI messages that will be handled before doSomething is called. Also, if doSomething is long running it will monopolize your UI thread and could cause a possible ANR force close. You may want to think about running doSomething in an AsyncTask if it takes more than 150ms or so.

Qberticus
Thanks for explaining this in detail.The method is actually initiating an OAuth handshake and then starts a new activity showing a WebView to perform the OAuth authorization.So, when the user clicks on the CheckBox, it takes some seconds before the WebView activity is started, because first a connection to Twitter is established to receive the request token. --> The reason I want to show the toast is to inform the user that something is going on in case he wanders why nothing seems to happen for some seconds (before the WebView activity is started / shown).
Manuel
Show an asynctask with a progress dialog
Falmarri