Progress Dialog is closed when touch on screen

Kailas picture Kailas · Oct 29, 2013 · Viewed 27.9k times · Source

I use a ProgressDialog in the thread. In the onButtonClick the thread is started, but when I touch anywhere on the screen the ProgressDialog is closed.

How can I prevent this?

private void ButtonClick(View view) {

    btn1 = (Button) view.findViewById(R.id.btn1);
    btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            GetStudentData();
        }
    });
}

private synchronized void GetStudentData() {  
    try {
        // Thread to display loader
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                dialog = Msg.ShowProgressDialogBox(
                    getActivity(),
                    dialog,
                    "Please wait while we fetch the student data...");
                Looper.loop();
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                String studentData="Kailas";
            }   
        }
    } catch(Exception ex {
    }
}

Update

When I touch on the screen the ProgressDialog disappears but get all data.

Answer

JJ86 picture JJ86 · Oct 29, 2013

Add this to your dialog:

yourDialog.setCanceledOnTouchOutside(false);

In this way you can touch screen and the Dialog will be not canceled.

EDIT

If you are using DialogFragment, then you must use the following code snippet before calling show(FragmentManager mng, String tag):

More info here.

dialogFragment.setCancelable(false);

EDIT 2

Be careful with ProgressDialog , because with Android Oreo (v8.0) it is now deprecated.