Jin Appsdev
Jin Appsdev

Reputation: 21

Why my TOAST is showing twice in post handler?

I want to show my toast message only once if the progressStatus reaches 100. Why is it that it is executed twice? Thank you for your kind responses.

new Thread(new Runnable() {
                        @Override
                        public void run() {
                            while (progressStatus < 100) {
                                // Update the progress status
                                progressStatus += 1;

                                // Try to sleep the thread for 50 milliseconds
                                try {
                                    Thread.sleep(50);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }       
                                handler.post(new Runnable(){

                                        @Override
                                        public void run() {
                                            pb = dialog.findViewById(R.id.no_connectiondProgressBar);
                                            pb.setProgress(progressStatus);

                                            if (progressStatus == 100) {
                                                
                                                final Toast toast = new Toast(getApplicationContext());
                                                toast.setDuration(Toast.LENGTH_LONG);
                                                View custom_view = getLayoutInflater().inflate(R.layout.custom_toast, null);
                                                toast.setView(custom_view);
                                                toast.show(); 
                                                dialog.dismiss();

                                            }
                                        }


                                    });
                            }
                        }
                    })

Upvotes: 1

Views: 196

Answers (1)

HMR
HMR

Reputation: 146

Move handler.post() to outside the while loop

        new Thread(new Runnable() {
        @Override
        public void run() {
            
            while (progressStatus < 100) {
                // Update the progress status
                progressStatus += 1;

                // Try to sleep the thread for 50 milliseconds
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            handler.post(new Runnable(){

                @Override
                public void run() {
                    pb = dialog.findViewById(R.id.no_connectiondProgressBar);
                    pb.setProgress(progressStatus);

                    if (progressStatus == 100) {

                        final Toast toast = new Toast(getApplicationContext());
                        toast.setDuration(Toast.LENGTH_LONG);
                        View custom_view = getLayoutInflater().inflate(R.layout.custom_toast, null);
                        toast.setView(custom_view);
                        toast.show();
                        dialog.dismiss();

                    }
                }


            });
        }
    })

Upvotes: 2

Related Questions