I have an activity that shows ProgressDialog while connecting to bluetooth device. It works fine until screen orientation changes while ProgressDialog is up. ProgressDialog disappears and when after connection is established, app calls progressDialog.dismiss(); at this point app crashes because there is no ProgressDialog to dismiss. How can i prevent to ProgressDialog dismissing while screen orientation changes?
public void prepareViews(int ID, boolean state){
switch(ID){
case USERNAME_TEXTBOX:
LoginUsernameTextBox.setEnabled(state);
break;
case PASSWORD_TEXTBOX:
LoginPasswordTextBox.setEnabled(state);
break;
case LOGIN_BUTTON:
LoginButton.setEnabled(state);
break;
case LOGIN_PROGRESSBAR:
if(state == true){
LoginProgressBar.setVisibility(View.VISIBLE);
LoginProgressBar.setIndeterminate(true); }
else{
LoginProgressBar.setVisibility(View.GONE);
}
break;
case CONNECTING_DIALOG:
if(state == true){
progressDialog = ProgressDialog.show(MainActivity.this, "", "Connecting", true); }
else{
progressDialog.dismiss();
}
break;
}
}
Tap the Settings icon to open the Settings app. Scroll down and tap Accessibility. Scroll down to Interaction controls and tap Auto-rotate screen to turn it off.
Keep in mind that Android destroys and recreates Activities when you rotate the device. Your problem is likely that progressDialog
is null
because after the Activity was recreated that variable was not set again.
You'll need to use a DialogFragment and keep in mind the activity lifecycle. What you need to do is keep track of whether the dialog was shown and display it again in OnCreate using a DialogFragment.
Previously it was done with Activity.dismissDialog and Activity.showDialog but those are deprecated now.
More info: using dialog fragments.
I end up to use DialogFragment and it works.
public class MainActivity extends Activity{
public void prepareViews(int ID, boolean state){
switch(ID){
case USERNAME_TEXTBOX:
LoginUsernameTextBox.setEnabled(state);
break;
case PASSWORD_TEXTBOX:
LoginPasswordTextBox.setEnabled(state);
break;
case LOGIN_BUTTON:
LoginButton.setEnabled(state);
break;
case LOGIN_PROGRESSBAR:
if(state == true){
LoginProgressBar.setVisibility(View.VISIBLE);
LoginProgressBar.setIndeterminate(true); }
else{
LoginProgressBar.setVisibility(View.GONE);
}
break;
case CONNECTING_DIALOG:
if(state == true){
showDialog();
}
break;
}
}
public void showDialog() {
FragmentManager fragmentManager = getFragmentManager();
ProgressDialogFragment newFragment = new ProgressDialogFragment();
newFragment.show(fragmentManager, "Dialog");
}
public static class ProgressDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final ProgressDialog progressDialog = ProgressDialog.show(getActivity(), "", "Connecting to "
+ DeviceName.subSequence(0, DeviceName.length() - 17));
return progressDialog;
}
}
}
Try this.
if ( progressDialog.isShowing()) {
progressDialog.dismiss();
}
here is the link android Docs
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