Spinner drop-down list and screen orientation change problem

后端 未结 3 2142
野性不改
野性不改 2020-12-19 14:03

I have a problem with a spinner drop down list and changing orientations.

In my activity, I display a dialog in which I have two spinners. When the dialog is shown t

相关标签:
3条回答
  • 2020-12-19 14:37

    The answer is here: app crashes when alert dialog is open and phone(emulator) changes its orientation . Look at answer from MegaMind to help resolve this.

    0 讨论(0)
  • 2020-12-19 14:42

    I had an idea to prevent orientation changes when dialog with spinner is displayed, but found that there is no reliable way to do that. The best solution that I found is to replace spinner with a button.

    0 讨论(0)
  • 2020-12-19 14:51

    This occurs because onDetachedFromWindow() does not get called on the spinner when dialog.dismiss() is called. The solution for this is to create a simple custom spinner class the exposes this method.

     public class DialogSpinner extends Spinner {
    
         public DialogSpinner(Context context, AttributeSet attrs) {
             super(context, attrs);
         }
    
         @Override
         public void onDetachedFromWindow() {
             super.onDetachedFromWindow();
         }
     }
    

    Now the onDetachedFromWindow() method is publicly available. Then in your dialog class override the onPause() method.

     @Override
     public void onPause() {
         mYourSpinner.onDetachedFromWindow();
         super.onPause();
     }
    

    It's definately a workaround, but it seems to do the trick.

    0 讨论(0)
提交回复
热议问题