开发者

Progress Dialog doesn't appear immediately

开发者 https://www.devze.com 2023-02-06 07:25 出处:网络
My progressDialog doesn\'t appear immediately when I start a new activity. I am using AsyncTask for the same. I am loading data from web service in next activity. Following is my async class :

My progressDialog doesn't appear immediately when I start a new activity. I am using AsyncTask for the same. I am loading data from web service in next activity. Following is my async class :

private class TheTask extends AsyncTask<Void, Void, Void>{

    Context con;
     Intent aboutusIntent;
     TabGroupActivity parentActivity;
    private TheTask(Context context)
    {
        this.con=context;
    }

    @Override
    protected void onPreExecute() {
        progDialog = ProgressDialog.show(con, "Lo开发者_开发百科ading... ",
                "please wait....", true);

    }

    @Override
    protected Void doInBackground(Void... params) {
         aboutusIntent = new Intent(con, Departments.class);
          parentActivity = (TabGroupActivity)getParent();
        //progDialog.show();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        parentActivity.startChildActivity("Departments", aboutusIntent);
        if(progDialog.isShowing())
        {
        progDialog.dismiss();
        }


    }

}  

I am creating instance of this class onClick of button :

ourdepbtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            new TheTask(AboutUs.this.getParent()).execute();
        }
    });  

Any suggestions?


Handler mHandler = new Handler();// This statement is to be called by the main thread  

ProgressDialog.show();// This statement is to be called by the main thread  

Thread t = new Thread(  
new Runnable(){  

public void run()  
{  

  callWebServicesHereAndFetchingMethod(); //  

   mHandler.post(new Runnable(){  

   public void run()  
   {  
     ProgressDialog.dismiss();  
   }   
});  
    }});  
t.start();  


Your Code Need Multithreading .... All the Visual Effect is controlled by your main thread. If you do any processing or say data fectching from web services using Main thread then progress dialog will not appear . Make the progress dialog show function called by the Main Thread. do all the fetching using another thread. make a thread that will join your fetching thread and Using Handler class object produce any visual effect you want to do

If you need this to be elaborated more. i will post that too


The user interface (UI) is not thread safe, calls to the UI must be made from the main thread (also called the "UI thread").

Handler handler = new Handler(); // Create on main thread.

// ...

handler.post(new Runnable() {
  @Override
  public void run() {
    ProgressDialog dialog = ProgressDialog.show(this, "",
       "Loading. Please wait...", true);
    }
});


What you are looking for is AsyncTask, where you can show/hide the ProgressDialog in onPreExecute()/onPostExecute()

You can read more about it here

0

精彩评论

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