How to raise a toast in AsyncTask, I am prompted to used the Looper
onPostExecute - executes on UI thread
or
publishProgress()
; in your doinbackground and
protected void onProgressUpdate(Integer... progress) {
}
http://developer.android.com/reference/android/os/AsyncTask.html
How to display a Toast in Android AsyncTask?
You cannot update UI on background thread. doInBackground()
is invoked on the background thread. You should update UI on the UI thread.
runOnUiThread(new Runnable(){
@Override
public void run(){
//update ui here
// display toast here
}
});
onPreExecute()
, onPostExecute(Result)
, are invoked on the UI thread. So you can display toast here.
onProgressUpdate(Progress...)
, invoked on the UI thread after a call to publishProgress(Progress...)
can be used to animate a progress bar or show logs in a text field.
The result of doInBackground()
computation is a parameter to onPostExecute(Result)
so return the result in doinBackground()
and show your toast in onPostExecute(Result)
You can also use a handler as suggested by @Stine Pike
For clarity, check the link below under the topic: The 4 steps.
http://developer.android.com/reference/android/os/AsyncTask.html
How to show toast in AsyncTask in doInBackground
You could wrap the Toast in runOnUIThread()
but this isn't the best solution.
You should set a boolean flag in the catch block when an error occurs, then display an appropriate Toast in onProgressUpdate()
, onPostExecute()
, or any of the other methods with UI access whenever the flag is true
.
Android: toast inside AsyncTask
I'm going t close this question. I haven't solved my problem but the number of answers provided that apparently work in other situations suggest there's something else going on. I'm going to re-structure the classes to get around having to call from within AsyncTask.
Problem with Toast in AsyncTask method call
The code in the doInBackground() method runs on its own thread so you cannot access any UI element from there directly as they are running on the UI thread.
So you got two options.
You handle all the UI stuff in the onPreExecute() and onPostExecute() method which run on the UI thread.
You handle UI stuff in the onProgressUpdate() method which also runs on the UI thread. You can trigger this method from within doInBackground() by calling publishProgress().
Uploading information using AsyncTask in android
In AsyncTask you have to notified in onPostExecute method:
So use this:
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Toast toast = Toast.makeText(YourActivity.this, "Message Here", duration).show();
pDialog.dismiss();
}
It also depends on your Response that which type of message do you want to display.
If you are not using AsyncTask then you can also use Handler.
Android AsyncTask [Can't create handler inside thread that has not called Looper.prepare()]
You are attempting to update the UI from a background thread. Either move the toast to onPostExecute
, which executes on the UI thread (recommended), or call runOnUiThread
.
runOnUiThread(new Runnable() {
public void run() {
// runs on UI thread
}
});
Display Toast from AsyncTask until event occurs
I'd suggest using a ProgressDialog instead of a Toast
. You can show it before running the AsyncTask
and hide it onPostExecute
. It'd look something like.-
ProgressDialog dialog = ProgressDialog.show(CurrentActivity.this, "", "Connecting...", true, false);
[...]
dialog.dismiss();
By the way, don't forget to put your string literals in a Strings
resources file :)
Related Topics
Outofmemory Exception When Loading Bitmap from External Storage
Android: Skimagedecoder:: Factory Returned Null
Bufferoverflowexception When Building Application
Google Play Services Out of Date. Requires 11011000 But Found 10289574
Why Isn't My Vector Drawable Scaling as Expected
Android Getting an Image from Gallery Comes Rotated
Okhttp Response Callbacks on the Main Thread
How to Add Custom Font in React Native Android
Center Align Title in Action Bar Using Styles in Android
Listview Items Are Not Clickable. Why
Android Options Menu Icon Won't Display
How to Disable Proguard for Building My Android App
Problem Loading Swf File in Android
Failure [Install_Failed_Update_Incompatible] Even If App Appears to Not Be Installed
Cordova Platform Add Android Not Working While Listing Android Targets