1

I want to show a Progress-Dialog before my view has been loaded. First i wrote the code in onCreate() but the dialog doesn't appear in that case. So i wrote it in onResume() but in this case, it doesn't disappear even after the view has been loaded. can anyone tell whats going wrong here?

              protected void onResume() {
    // TODO Auto-generated method stub

    super.onResume();
    dialog = ProgressDialog.show(this, "", "Please wait...", true); 
    //dialog.cancel();
    new Thread() 
    {
      public void run() 
      {

         try
           {

            sleep(1500);

      // do the background process or any work that takes time to see progress dialog

           }  
        catch (Exception e)
        {
            Log.e("tag",e.getMessage());
        }
    // dismiss the progressdialog   
     dialog.dismiss();
     }
    }.start();
    citySelected.setText(fetchCity);
    spinner.setSelection(getBG);
}
Chetna
  • 165
  • 1
  • 6
  • 15

3 Answers3

1

You cant update UI(which is in main UIthread) from other threads. If you want to run any query in the background, you can use AsyncTask.

In onPreExecute method, show dialog and onPostExecute you can dismiss the dialog.

If you want to use Thread, then update UI using handlers.

Using AsyncTask

public class MyAsyncTask extends AsyncTask<String, Void, String> {
    ProgressDialog dialog = new ProgressDialog(ActivityName.this);
    @Override
    protected void onPreExecute() {
        dialog.show();
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        dialog.dismiss();
        super.onPostExecute(result);
    }

}

In Activity onCreate Method,

MyAsyncTask task = new MyAsyncTask();
    task.execute();
Raghu Nagaraju
  • 3,278
  • 1
  • 18
  • 25
  • I tried implementing this, but it pauses the screen to the same activity and then the next view is loaded without the dialog being shown! – Chetna Jun 25 '12 at 09:01
  • if you want to start new activity after completing this, then start new activity in onPostExecute method. Dont call in oncreate itself. I think, you are calling startActivity in onCreate after task.execute(). Put it in onPostExecute – Raghu Nagaraju Jun 25 '12 at 10:05
0

You can use AsyncTask. It is better than Thread

private class DownloadingProgressTask extends
        AsyncTask<String, Void, Boolean> {

    private ProgressDialog dialog = new ProgressDialog(ShowDescription.this);
    protected void onPreExecute() {
         this.dialog.setMessage("Please wait");
         this.dialog.show();

    }
    protected Boolean doInBackground(final String... args) {
        try {
                downloadFile(b.getString("URL"));
                return true;
        } catch (Exception e) {
                Log.e("tag", "error", e);
                return false;
        }
    }
    @Override
    protected void onPostExecute(final Boolean success) {

         if (dialog.isShowing()) {
         dialog.dismiss();
         }
    }
}
Nirali
  • 13,571
  • 6
  • 40
  • 53
0

better to use Asynctask ......... but if you still want same or want to know the solution only then can try

new Thread() 
    {
      public void run() 
      {

         try
           {

            sleep(1500);

      // do the background process or any work that takes time to see progress dialog

           }  
        catch (Exception e)
        {
            Log.e("tag",e.getMessage());
        }




  YourActivity.this.runOnUIThread(new Runnable(){
                         @Override
                         public void run(){
                             // dismiss the progressdialog   
                              dialog.dismiss();
                        });





     }
    }.start();
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36