Android: How can I pass parameters to AsyncTask's onPreExecute()?

前端 未结 4 1960
轻奢々
轻奢々 2020-12-02 04:32

I use an AsyncTask for loading operations that I implemented as an inner class.

In onPreExecute() I show a loading dialog which I then hid

4条回答
  •  再見小時候
    2020-12-02 05:10

    You can override the constructor. Something like:

    private class MyAsyncTask extends AsyncTask {
    
        public MyAsyncTask(boolean showLoading) {
            super();
            // do stuff
        }
    
        // doInBackground() et al.
    }
    

    Then, when calling the task, do something like:

    new MyAsyncTask(true).execute(maybe_other_params);
    

    Edit: this is more useful than creating member variables because it simplifies the task invocation. Compare the code above with:

    MyAsyncTask task = new MyAsyncTask();
    task.showLoading = false;
    task.execute();
    

提交回复
热议问题