简体   繁体   中英

async task not running when called from other async task

class TestAsync extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
    v.findViewById(R.id.loadinglayout).setVisibility(View.VISIBLE);
    v.findViewById(R.id.wholecontentlayout).setVisibility(View.INVISIBLE);
    super.onPreExecute();
}

@Override
protected Void doInBackground(Void... voids) {
    callAPI();
    return null;
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    v.findViewById(R.id.loadinglayout).setVisibility(View.INVISIBLE);
    v.findViewById(R.id.wholecontentlayout).setVisibility(View.VISIBLE);
}
}



public void callAPI ()
{
    RequestInterface requestInterface = new RequestImplementation();
    requestInterface.setUrl("https://api.myjson.com/bins/vl9pp");
    ConnectionExecutor connectionExecutor = new 
    ConnectionImplementation();
    ResponseInterface temp = null;
    try {
       temp = connectionExecutor.executeConnection(requestInterface);
    } catch (InterruptedException e) {
       e.printStackTrace();
    } catch (ExecutionException e) {
       e.printStackTrace();
    }
   rangeText.setText(temp.getPayload());
}

Call API function will call a method of another class where HttpConnection is done.

If the function is directly called from onViewCreated() in fragment then it works. If I call it from another async task it returns nothing.

I am actually trying to show a progress bar when the callApi function is called.

call it on onPostExecute method

@Override
 protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
v.findViewById(R.id.loadinglayout).setVisibility(View.INVISIBLE);
v.findViewById(R.id.wholecontentlayout).setVisibility(View.VISIBLE);
  callAPI();
}}

It is not allowed to start an AsyncTask outside the UI thread. doInBackground is outside the UI thread.

From https://developer.android.com/reference/android/os/AsyncTask.html :

Threading rules

...

The AsyncTask class must be loaded on the UI thread.

From this SO answer

only inside onProgressUpdate() or onPostExecute() since these methods runs on the UI thread. Therefore, start the second AsyncTask on the UI thread by choosing one of the two methods listed above.

Simply put, if you're in need of firing up AsyncTask within another AsyncTask, it must be called from either onProgressUpdate() or onPostExecute() since they resides in UI thread.

To show the ProgressBar to illumilate the task is running, you should show your progress bar in onPreExecute() and hide it in onPostExecute(Result) .

 AsyncTask<Void, Void, Void> yourTask = new AsyncTask<Void, Void, Void>() {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        /**
         * Show your progress bar:
         */
        yourProgressBar.setVisibility(View.VISIBLE);

    }

    @Override
    protected Void doInBackground(Void... params) {
        /**
         * Do your stuffs in background thread
         */
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        /**
         * Hide your progress bar:
         */
        yourProgressBar.setVisibility(View.GONE);
    }
};

Run the task: yourTask.execute();

onPostExecute(Result) method will get called when the task completed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM