Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show toast in AsyncTask in doInBackground

In one of my activities I'm using AsyncTask. In doInBackground() I'm making calls to various methods. In one of these methods I'm getting an exception, so in the catch block I want to show the error in the Toast. I know I can use Log, but still I prefer Toast. So, how can I use Toast in AsyncTask in doInBackground()?

like image 877
reiley Avatar asked Dec 09 '12 18:12

reiley


People also ask

How do I show toast in AsyncTask?

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 .

How do I show toast in another activity?

A simpler way to do this is to overwrite the onStop and use getApplicationContext() . Then the toast will appear when the activity closes.

How do you show toast in thread?

myActivity. runOnUiThread(new Runnable() { public void run() { Toast. makeText(myActivity, "Toast Message Text!", Toast.

Can I show toast from background thread?

Steps: declare a handler in the main activity (onCreate) now create a thread from the main activity and pass the Context of that activity. now use this context in the Toast, instead of using getApplicationContext()

How do I show Toast in doinbackground?

show your Toast in onPostExecute or onPreExecute. doInBackGround runs on a separate thread but the other two methods run on the UI thread. But if it is must to show toast in doInBackGround then you can use Handler.post or runonUiThread to perform toast showing.

What is asynctask in Android?

Looking to earn? AsyncTask (asynchronous task) is an Android class that makes it easier to do operations on a background thread and publish the result on the User Interface (UI)/ main thread without having to manipulate threads and handlers ourselves. In Android, updating a UI from a separate thread is a frequently encountered scenario.

How to display toast in a non-UI thread?

But if it is must to show toast in doInBackGround then you can use Handler.post or runonUiThread to perform toast showing. You can't display Toast in non-UI thread (i.e Do in Background).You may try the flag concept.

How to get toast to be displayed when error occurs?

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.


1 Answers

return from doInBackground as

protected String doInBackground(String... params){
    //some code
    try{
       //some code
     }catch(Exception e){
        return "Exception Caught";
     }
     return someValidResult;
}

protected void onPostExecute(String result){
    if(result.equalsIgnoreCase("Exception Caught")){
       //Display Toast
    }else{
       // // whatever you wana do with valid result
    }
}
like image 117
Robin Chander Avatar answered Nov 08 '22 21:11

Robin Chander