开发者

AsyncTask not locking main thread?

I'm using following code to fill a custom ListPreference dialog. Since the fill procedure takes a lot of time i want to show a progress dialog during the fill procedure.

My problem is that filler.execute() does not block onPrepareDialogBuilder and functions goes till the end before values are filled causing an exception... Any idea?

@Override
protected void onPrepareDialogBuilder(Builder builder) {        
    // Load data
    if (this.g开发者_Python百科etEntries()==null) {
        FillerTask filler = new FillerTask();
        filler.execute();
    }   
    Log.d(TAG, "Filler finished");
    super.onPrepareDialogBuilder(builder);
}

Here is Filltertask code, basically he looks for every activity with a MAIN Intent filling a list:

private class FillerTask extends AsyncTask<Void, Void, String[][]> {
    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        Log.d(TAG, "Dismiss dialog");
        dialog = ProgressDialog.show(MyListPreference.this.getContext(), "", "Doing stuff...", true);
    }

    @Override
    protected String[][] doInBackground(Void... params) {
        return fill();
    }

    public String[][] fill() {
    Log.d(TAG, "Fill started");

        CREATE LISTS...

        // Done
        Log.d(TAG, "Fill done");
        String[][] result = new String[][] {entryNames, entryValues};
        return result;
    }


    @Override
    protected void onPostExecute(String[][] result) {
        Log.d(TAG, "Post execute");
        MyListPreference.this.setEntries(result[0]);
        MyListPreference.this.setEntryValues(result[1]);
        dialog.dismiss();
    }
 }  


My problem is that filler.execute() does not block onPrepareDialogBuilder and functions goes till the end before values are filled causing an exception... Any idea?

That is the entire point behind an AsyncTask. The "Async" in AsyncTask means asynchronous.

Use your AsyncTask to get your data. Then, in onPostExecute(), display the dialog.


Found the solution, best way to do this is override the onClick method and let the AsyncTask postExecute call the "super()", so click is not passed until content is loaded and during load progress bar is correctly displayed.


asyntask doesn't lock main thread, it just drops a message to message queue of main thread

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜