doInBackground run so fast when using AsyncTask in Fragment

风流意气都作罢 提交于 2020-01-17 03:08:25

问题


I have a problem so strange when using Asynctask in fragment,

In onCreate of Fragment I called AsyncTask like so:

@Override
public void onCreate(Bundle savedInstanceState) {
    // Step 1 
    new FitnessHistoryDataAsync(getActivity()).execute(10);

    super.onCreate(savedInstanceState);
}

and the doInBackground method:

@Override
protected Boolean doInBackground(Integer... params) {
    // Step 2 
    Log.i("", "doInBackground");

    Constants.SDK.getFitnessHistoryData(context, 10,
            new FitnessHistoryListener() {

        @Override
        public void onReceiveFailed(String errorMessage) {
             // Step 4 
            Log.i("", "error " + errorMessage);
        }

        @Override
        public void onReceiveData(
                FitnessHistory[] fitnessHistoryData) {
            // Step 4 
            Log.i("", "onReceiveData");
        }
    });

    return true;
}

and the onPostExecute method:

@Override
protected void onPostExecute(Boolean result) {
    super.onPostExecute(result);

    // Step 3 
    Log.i("", "onPostExecute");

            // Hide progress bar dialog
    Constants.connectivity.hideProgressDialog();
}

My Asynctask ran as followed: Step 1, Step 2, Step 3, then Step 4.

The problem is in the doInBackground method where Step 2 and Step 4 cannot run at the same time before running Step 3 in the onPostExecute method.

I want everything in the doInBackground method to run before going to the onPostExecute method.

Please help me.

Thanks.

EDIT :

I changed to the following code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

            // Step 1 
    new FitnessHistoryDataAsync(getActivity()).executeonExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 10);
}

Although I used AsyncTask.THREAD_POOL_EXECUTOR or AsyncTask.SERIAL_EXECUTOR to try to fix this problem, but cannot.


回答1:


Calling new FitnessHistoryDataAsync(getActivity()).execute(10); before super.onCreate(savedInstanceState); is a problem you should fix first.
Change it to something like this

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Step 1 
    new FitnessHistoryDataAsync(getActivity()).execute(10);

}



回答2:


It looks like the library I used also is another thread. So, I no need to create new thread AsyncTask.

Just edit coding like this :

Constants.SDK.getFitnessHistoryData(context, 10,
        new FitnessHistoryListener() {

    @Override
    public void onReceiveFailed(String errorMessage) {
         // Step 4 
        Log.i("", "error " + errorMessage);
    }

    @Override
    public void onReceiveData(
            FitnessHistory[] fitnessHistoryData) {
        // Step 4 
        Log.i("", "onReceiveData");

        // TODO Directly updated UI in here when already received new data
    }
});

That's solved my issue.



来源:https://stackoverflow.com/questions/22889968/doinbackground-run-so-fast-when-using-asynctask-in-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!