For example, I have the following class:
public class MyClass{
private static int x;
private AsyncWorker worker;
public void myVoid(){
worker = new Worker;
worker.execute();
if (x == null){
Log.v("Say something: ", "x is null");
}
}
class AsyncWorker extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
x = 10;
return null;
}
@Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
}
}
}
At the result, it prints that x is null. Is there any way to change x from AsyncTask and use it’s value in future?
You need to read up on threading and the user of ASyncTask. When you call execute on your AsyncTask, myVoid goes straight on to the next line as ASyncTask is running on a different thread, so x will always be null as the AsyncTask won’t have done anything yet. You’re also introducing problems here with crossthreading and variable synchronisation.
In the nicest way possible, I would recommend that you take the question down and find a decent tutorial on the use of AsyncTask and threads