Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent ProgressDialog to dismissing on screen rotation change in Android?

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;
        }
    }
like image 983
user1888162 Avatar asked Jan 24 '13 20:01

user1888162


People also ask

How do I stop screen Swivelling?

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.


3 Answers

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.

like image 114
frozenkoi Avatar answered Nov 15 '22 00:11

frozenkoi


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;
        }
    }
}
like image 35
user1888162 Avatar answered Nov 15 '22 01:11

user1888162


Try this.

if ( progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }

here is the link android Docs

like image 31
Qadir Hussain Avatar answered Nov 15 '22 00:11

Qadir Hussain