0

I have a class which generates a pdf file. This class is extended by Activity that means is have onCreate, onPause (and so on) methods.

While a pdf file is generating it takes some time to be generated so I need to show a progress bar to notify the user that the file is generating at the moment.

Everything works fine expect that when I click on a generate button the progress bar freezes for a few seconds (according to Logcat: progress bar freezing while file is generating).

So my problem is that, that I don't know how to make the progress bar unstoppable while the file is generating.

Below is my code:

Log.i(TAG, "PDF generation: " + position);

final ProgressDialog dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setIndeterminate(true);
dialog.setMessage("Generating...");
dialog.show();

new Thread(new Runnable() {

    @Override
    public void run() {

        startActivity(new Intent(getApplicationContext(),
            PdfGenerator.class));

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                dialog.hide();
            }
        });

    }

}).start();
burjulius
  • 279
  • 3
  • 17

1 Answers1

1

Change your code as for starting an Activity from Thread using runOnUiThread:

///... your code here..

    new Thread(new Runnable() {

        @Override
        public void run() {

            Your_Current_Activity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {

                dialog.cancel();

                // START ACTIVITY HERE
               Your_Current_Activity.this.startActivity(new 
                        Intent(Your_Current_Activity.this,
                                       PdfGenerator.class));

                }
            });

        }

    }).start();
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Now it looks like good but I did some changes. I have changed dialog.hide() to dialog.cancel() and moved after starting a new activity. Thanks anyway! – burjulius Dec 08 '12 at 18:46
  • @juliuks : but `runOnUiThread `is method of Activity class then without using Activity context it's not possible to access it.most welcome firend :) – ρяσѕρєя K Dec 08 '12 at 18:49
  • I think you didn't understand me what changes I did. Btw, the code now looks like: http://pastebin.com/fxB1Zz9i – burjulius Dec 08 '12 at 19:21
  • @juliuks : yes you are doing right remember one point `Stats.this` is important when you want to access `runOnUiThread` – ρяσѕρєя K Dec 08 '12 at 20:15