What is the proper way to handle orientation change when a custom alertdialog has an open spinner?

前端 未结 3 1098
星月不相逢
星月不相逢 2021-01-05 13:42

In my app I have a custom AlertDialog (handled by the system using showDialog()) which contains a tabhost with 2 tabs. In one of the tabs is a spinner. I can rotate my scr

3条回答
  •  Happy的楠姐
    2021-01-05 14:31

    I found a better solution. It is actually not so hard to replace Spinner with a button that looks and behaves like Spinner (except the crash, fortunately).


    public void showSpinner() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle(_spinnerPrompt);
        builder.setSingleChoiceItems(_spinnerOptions, _spinnerSelection,
          new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                _spinnerSelection = item;
                _pseudoSpinner.setText(_spinnerOptions[item]);
                _restoreSpinnerOnRestart = false;
                dialog.dismiss();
            }
        });
        builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                _restoreSpinnerOnRestart = false;
            }
        });
        AlertDialog alert = builder.create();
        _restoreSpinnerOnRestart = true;
        alert.show();
        }
    
    @Override
    public Bundle onSaveInstanceState() {
        Bundle state = super.onSaveInstanceState();
        state.putBoolean(STATE_SPINNER_RESTORE, _restoreSpinnerOnRestart);
        state.putInt(STATE_SPINNER_SELECTION, _spinnerSelection);
        return state;
    };
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        _spinnerSelection = savedInstanceState.getInt(STATE_SPINNER_SELECTION, -1);
        _restoreSpinnerOnRestart = savedInstanceState.getBoolean(STATE_SPINNER_RESTORE);
        _pseudoSpinner.setText(_spinnerOptions[_spinnerSelection]);
    
        if (_restoreSpinnerOnRestart) {
            showSpinner();
        }
    };
    

提交回复
热议问题