How to prevent rejection of version of ProgressDialog on screen change screen in Android?

I have an activity that shows a ProgressDialog when connected to a Bluetooth device. It works fine until the screen orientation changes, but ProgressDialog. ProgressDialog disappears, and when after establishing the connection, the application calls progressDialog.dismiss (); In this case, the application crashes due to the fact that the ProgressDialog does not start. How can I prevent the ProgressDialog from declining while changing the screen orientation?

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;
        }
    }
+5
source share
5 answers

I end up using 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;
        }
    }
}
+2
source

, Android . , , progressDialog null, , , .

DialogFragment . , , OnCreate "".

Activity.dismissDialog Activity.showDialog, .

: .

+7

.

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

+1

. , , , android: coinfigChange = "orientation". onConfigChange.

0

onConfigurationChanged() android:coinfigChange="orientation" .

, :

@Override
public void onConfigurationChanged(Configuration newConfig){

    if(!isConnection){    
        super.onConfigurationChanged(newConfig);
        setContentView(R.layout.main);
    }        
}

setContentView(), .

0

All Articles