0

I've created a generic AsyncTask. It works fine in background but when I update UI in doInBackground callback of this task, it just hang loading circle of SwipeRefreshLayout multiple times. I need this loading to run smoothly. I'm using AsyncTask as follows

public class backgroundWorker extends AsyncTask<Object, Void, Object>{//params, progress, result
    backgroundWorkerCallback callback;
    public backgroundWorker(backgroundWorkerCallback _callback)    {
        this.callback = _callback;
    }
    @Override
    protected void onPostExecute(Object result) {
        super.onPostExecute(result);
        callback.onPostExecute(result);
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected Object doInBackground(Object... param)
    {
        return callback.doInBackground(param);
    }
}

backgroundWorkerCallback is an interface

public interface backgroundWorkerCallback {

    public Object doInBackground(Object param);
    public void onPostExecute(Object result);
}

I'm using this AsyncTask in this way

srlStockReport.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        backgroundWorker bw = new backgroundWorker(new backgroundWorkerCallback(){
            @Override
            public Object doInBackground(Object param)
            {
                //fetch data from server
                //put some data in in Preference
                //Load a RecyclerView from list

                return null;
            }
            @Override
            public void onPostExecute(Object result)
            {

            }
        });
        bw.execute();
    }
});
Inaam ur Rehman
  • 470
  • 1
  • 6
  • 23
  • Refer to [this](https://stackoverflow.com/a/15757788/7915814) answer, and actually to [this](https://stackoverflow.com/a/7453156/7915814) afterwards – Suleyman May 08 '18 at 22:06
  • Possible duplicate of [Doing UI task in doinbackground() in Android](https://stackoverflow.com/questions/15757251/doing-ui-task-in-doinbackground-in-android) – Suleyman May 08 '18 at 22:08

0 Answers0