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

前端 未结 3 1095
星月不相逢
星月不相逢 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条回答
  •  失恋的感觉
    2021-01-05 14:25

    I had a similar a crash. I worked around by disabling orientation changes when dialog is displayed.

    @Override
    public void onDismiss(DialogInterface dialog) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }
    
    @Override
    public void onShow(DialogInterface dialog) {
        int loadedOrientation = getResources().getConfiguration().orientation;
        int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
        if (loadedOrientation == Configuration.ORIENTATION_LANDSCAPE) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        } else if (loadedOrientation == Configuration.ORIENTATION_PORTRAIT) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        }
        setRequestedOrientation(requestedOrientation);
    }
    

    NOTE: I actually found that there is no reliable way to lock screen orientation.

提交回复
热议问题