-1

I am not writing my whole code, I have the following simple structure:

public class CreateEventActivity extends ActionBarActivity{
   int x; 
   void onCreate(){
      new AsyncTask1().execute();//here I change the value of x
      Log.i("000000",String.valueOf(x));
   }
   public AsyncTask1 extends AsyncTask<Void, Void ,Void>{
    // process include changing the vlaue of x to 4 
     Log.i("111111",String.valueOf(x));
     }
}

in the log: the log with the tag 000000 appears before the log with tag 111111 what's going on?

First I thought the problem was because I am chainging the value of x in onPostExecute so I did so in doInBackground and the problem still the same.

Mark Khateeb
  • 907
  • 3
  • 11
  • 26
  • 1
    ASync tasks run the code in parallel on another thread. That's the entire point. The order it runs in vs code in the function it was executed from is undefined and may change each time it runs. – Gabe Sechan Mar 25 '15 at 23:13

2 Answers2

1

That's the nature of an Async task. Async Tasks are mostly being used for long running operations; like webcalls, I/O operations and so on as they can take a while.

If you want to have a callback when the AsyncTask finishes you can override the OnPostExecute() method

timr
  • 6,668
  • 7
  • 47
  • 79
  • is there anyway to avoid this? – Mark Khateeb Mar 25 '15 at 23:13
  • 1
    Yes like I said implement the OnPostExecute from the AsyncTask class. You can make a static property in your Activity that you set in that method. When it's gets set you call your Log.i method. – timr Mar 25 '15 at 23:16
1

what's going on?

AsyncTask is asynchronous. Quoting the documentation:

This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

The reason that you use an AsyncTask is to do work in a background thread (e.g., download some data), followed by some work on the main application thread (e.g., update the UI based upon that data).

is there anyway to avoid this?

Get rid of the AsyncTask, or learn how to use it properly. Among other things:

  • Do not modify the same data in multiple threads without appropriate synchronization logic

  • Do not try to fork a background thread, then block the main application thread (which appears to be what you want), as that eliminates the entire point of the background thread in the first place

If you have work that you want to do when doInBackground() ends, put that work in onPostExecute(), not in statements that follow your execute() or executeOnExecutor() call.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you, that make sense! however, I am using AsyncTask because I need a value (which is in a database) to initialize an View, what I can do then? – Mark Khateeb Mar 25 '15 at 23:29
  • @MahmoudKhateeb: Initialize the view in `onPostExecute()`. That is called on the main application thread, at which point it is safe for you to update your UI. – CommonsWare Mar 25 '15 at 23:32