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.
ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar , which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress.
getText(R. string. msg_dlg_analyse_pic), true, //indeterminate true, new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { m_bRunning = false; } });
You can use dialog. setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog.
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.
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...");
dialog.setCanceledOnTouchOutside(getRetainInstance());
Looper.loop();
}
}.start();
new Thread() {
@Override
public void run() {
String studentData="Kailas";
}
}
}
I Have used this line dialog.setCanceledOnTouchOutside(getRetainInstance()); and it worked fine in my android device OS(4.0.1) also tested on OS(2.6.3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With