0

I have a AsyncTask<Task, Void, Boolean> thread in my Android application. And I want to show message through Toast.makeText() when this thread completes its execution. For this I have added Toask.makeText() inside if as well as inside else of doInBackground method. The thread is completing its execution succesfully but the toast's message does not appears. So what can be the problem?

Code:

@Override
    protected Boolean doInBackground(Task... arg0) {
        try {

            Task task = arg0[0];

            QueryBuilder qb = new QueryBuilder();

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost request = new HttpPost(qb.buildContactsSaveURL());

            StringEntity params =new StringEntity(qb.createTask(task));
            request.addHeader("content-type", "application/json");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);

            if(response.getStatusLine().getStatusCode()<205)
            {
                /*this is the message inside if*/
                Toast.makeText(context, "inside -IF", Toast.LENGTH_SHORT).show();
                return true;
            }
            else
            {
                /*this is the message inside else*/
                Toast.makeText(context, "inside -ELSE", Toast.LENGTH_SHORT).show();
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
Sajal Ali
  • 427
  • 1
  • 10
  • 21
  • Possible duplicate of [How to show toast in AsyncTask in doInBackground](http://stackoverflow.com/questions/13790351/how-to-show-toast-in-asynctask-in-doinbackground) – Hitesh Sahu May 03 '16 at 13:34

4 Answers4

3

Toast work in Main thread you are trying to show Toast in Background Thread (doInBackground). Move your toast code to onPostExecution callaback and you will be able to see Toasts.

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
0

The Task it is doing is in background, it won't show toast as it is in background. Background tasks don't affect your UI or main thread.

VatsalSura
  • 926
  • 1
  • 11
  • 27
0

The thread is completing its execution succesfully but the toast's message does not appears

Because doInBackground method run on non-ui-Thread. and application only show Alert,Toast and update UI elements from UI-Thread only.

To show Toast from doInBackground wrap Toast related code inside runOnUiThread method

OR

return response from doInBackground method and use onPostExecute method to show Toast.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

As mentioned by other people, you shouldn't have any UI related changes/activities on the background thread. Do it on the main thread which onPostExecute method does. Here's an example

private class DoSomethingTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        //Do background process here. Make sure there are no UI related changes here
        return null;
    }

    protected void onPostExecute(Void x) 
    {
        //Do UI related changes here
    }
}

Using your code:

private class DoSomethingTask extends AsyncTask<Void, Void, Void> {
    int statusCode;
    @Override
    protected Void doInBackground(Task... arg0) {
        try {

        Task task = arg0[0];

        QueryBuilder qb = new QueryBuilder();

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost request = new HttpPost(qb.buildContactsSaveURL());

        StringEntity params =new StringEntity(qb.createTask(task));
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        HttpResponse response = httpClient.execute(request);
        statusCode = response.getStatusLine().getStatusCode();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    protected void onPostExecute(Void x) 
    {
        //Do UI related changes here

        if(statusCode < 205)
        {
            /*this is the message inside if*/
            Toast.makeText(context, "inside -IF", Toast.LENGTH_SHORT).show();
            return true;
        }
        else
        {
            /*this is the message inside else*/
            Toast.makeText(context, "inside -ELSE", Toast.LENGTH_SHORT).show();
            return false;
        }
    }
}

Hope this helps!

Luke Villanueva
  • 2,030
  • 8
  • 44
  • 94