-3

I am new in Android development. I am trying to show a ProgressDialog. I see lots of tutorial that say for showing dialog must use thread. As you can see snippet code is using thread.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            refreshFromFeed();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        setContentView(R.layout.activity_main);
    }

private void refreshFromFeed() throws InterruptedException {
        ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep");
        Thread th = new Thread(){
            public void run(){
                Log.d("TimeFrom", String.valueOf(System.currentTimeMillis()/1000));
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                Log.d("TimeTo", String.valueOf(System.currentTimeMillis()/1000));
            }
        };
        th.start();
        dialog.dismiss();
    }
protected void onRefresh(View view) throws InterruptedException {
        refreshFromFeed();
    }

The log shows it took 5 second, however, I cannot see any dialog on my screen and I can do anything on the screen. Even I use on a physical device. I've used debugging mode. There is no exception.

onRefresh is an event by onClick that declared on it's xml.

Charuක
  • 12,953
  • 5
  • 50
  • 88
Amir Shabani
  • 690
  • 2
  • 14
  • 36

4 Answers4

0

I've made a little bit changes of your code read it carefully.

private void refreshFromFeed() throws InterruptedException {
    ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep");
    Thread th = new Thread(){
        public void run(){
            Log.d("TimeFrom", String.valueOf(System.currentTimeMillis()/1000));
            try {
                Thread.sleep(5000);
                dialog.dismiss();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            Log.d("TimeTo",String.valueOf(System.currentTimeMillis()/1000));
        }
    };
    th.start();

}
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
0

You are dismissing your dialog just right after showing it. Maybe you want to move your "dialog.dismiss();" to inside the thread. Remember that you need to dismiss the dialog on UI Thread, otherwise it will crash your app:

private void refreshFromFeed() throws InterruptedException {
        final ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep");
        Thread th = new Thread(){
            public void run(){
                Log.d("TimeFrom", String.valueOf(System.currentTimeMillis()/1000));
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                Log.d("TimeTo", String.valueOf(System.currentTimeMillis()/1000));

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dialog.dismiss();
                    }
                });
            }
        };
        th.start();
    }

I see lots of tutorial that say for showing dialog must use thread.

You don't explicitly need a thread to show a ProgressDialog, this is just an example to dismiss it after 5000 ms

francisco_ssb
  • 2,890
  • 1
  • 17
  • 23
0
 private void refreshFromFeed() throws InterruptedException {
        final ProgressDialog dialog = ProgressDialog.show(getActivity(),"Loading","Wake up after some sleep");
        Thread th = new Thread() {
            public void run() {
                Log.d("TimeFrom", String.valueOf(System.currentTimeMillis() / 1000));
                try {
                    Thread.sleep(5000);
                    dialog.dismiss(); // dismiss your dialog here
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                Log.d("TimeTo", String.valueOf(System.currentTimeMillis() / 1000));
            }
        };
        th.start();
    }
Parth Shah
  • 64
  • 3
0

You still run the ProgressDialog in your UI thread.

ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep");

New thread created after this line, not before this line!

navylover
  • 12,383
  • 5
  • 28
  • 41