Progress Dialog for AsyncTask - Android

前端 未结 4 810
情深已故
情深已故 2021-01-06 19:16

I\'m trying to show a progress dialog while the twitter feed is loading up...However the progress dialog remains on screen when the twitter feed appears. Any help is much ap

4条回答
  •  爱一瞬间的悲伤
    2021-01-06 20:09

    You must call ProgressDialog show() method on AsyncTasks onPreExecute(). For example:

    class MyTask extends AsyncTask {
     ProgressDialog pd;
        @Override
        protected void onPreExecute() {
          super.onPreExecute();
           pd = new ProgressDialog(MainActivity.this);
           pd.setMessage("loading");
           pd.show();
        }
    
        @Override
        protected Void doInBackground(Void... params) {
          // Do your request
        }
    
        @Override
        protected void onPostExecute(Void result) {
          super.onPostExecute(result);
          if (pd != null)
          {
             pd.dismiss();
          }
        }
      }
    

提交回复
热议问题