开发者

cannot display progress bar at the time of loading another Activity in Android

This may be a simple question but i am a beginner ,i need your suggestion on this. i have two Activities A1 and A2 .When i click the image on A1 screen i have to display progress bar until A2 screen appears(A2 activity has huge task to do).I tried

 image.setOnClickListener(new ImageView.OnClickListener() {

            public void onClick(View v)
            {

             myProgressDialog = Progre开发者_运维问答ssDialog.show(A1.this,     
                        "Please wait...", "Loading...", true);

              new Thread() {
                   public void run() {
                        try{



                        Intent i = new Intent(A1.this,.A2.class);
                        startActivity(i);


                        } catch (Exception e) {  }
                        // Dismiss the Dialog
                        myProgressDialog.dismiss();
                   }
              }.start();


             }
        });

This couldn't display progress bar .I know that i am making a mistake but i couldn't figure out


Your best bet is to show the progress dialog in the A2 activity. Once you start an Activity, the previous activity goes into the background, so the progress dialog wouldn't display anyway.


First of all , the progress dialog needs to be called on a separate thread . Use the AsyncTask<> to display the dialog and at the same time perform some operation in the background . A sample code might be something like this

class hello extends AsyncTask<Void,Void,Void>
{
       ProgressDialog dialog=null;
       Intent i;

   @Override
   protected void onPreExecute() 
   {


           dialog=ProgressDialog.show(A1.this,"PLEASE WAIT","LOADING CONTENTS  ..",true);
   }

    @Override
    protected void onPostExecute(Void result) 
    {
            if(dialog.isShowing())
                    {
                       dialog.dismiss();
                       startActivity(i);
                    }           
    }

    @Override
    protected Void doInBackground(Void... params) 
    {
        i = new Intent(A1.this,.A2.class);
                    return null;
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜