burjulius
burjulius

Reputation: 289

Start an activity from the thread

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();

Upvotes: 0

Views: 208

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

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();

Upvotes: 1

Related Questions