How to keep Popup window opened when orientation changes at run time in Android?

前端 未结 4 1968
夕颜
夕颜 2020-12-19 07:12

I have created a Popup window which contains month view to pick up date. When I changes orientation, due to Android loads an activity all over again my popup Window gets dis

相关标签:
4条回答
  • 2020-12-19 07:30

    Add this property to your activity in manifest.xml

    android:configChanges="orientation|keyboard" 
    

    and that should do it.

    0 讨论(0)
  • 2020-12-19 07:34

    Whenever there is an orientation change, Android destroys your activity ( calls onDestroy()) and then restarts it (calls onCreate()).
    As soon as your popup is up, set a flag popup_open=1. Your popup will naturally have a dismiss button. Set the flag=0 in the click handler of this button. You can then re-open the popup when the app restarts in the method onRestoreInstanceState() or in the onCreate(). Here you would make a check for the flag. If the flag is set to 1, bring up the popup. So even if the orientation changed while the popup was up, onRestoreInstanceState() will know what to do based onthe state of the flag. For more reference check: How to handle runtime changes.

    0 讨论(0)
  • 2020-12-19 07:35

    include android:configChanges="orientation" in your AndroidManifest.xml to the activity displaying window. Doing this tells android that you are going to handle orientation change yourself and eventually it will not destroy your activity and keeping the window displayed.

    This technique is good if you dont have different layouts for portrait and landscape mode. However, if you do, you may still perform custom layout implementation by detecting the orientation mode as below:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
            Log.i("orientation", "Orientation changed to: Landscape");
        else
            Log.i("orientation", "Orientation changed to: Portrait");
    }
    

    for preview, download and install this sample app.

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

    Showing PopupWindow as

    final View parent = findViewById(R.id.{parentId});
    parent.post(new Runnable() {
        @Override
        public void run() {
            mPopup.showAtLocation(parent, ...);
        }
    });
    

    resolves unhandled exception on orientation change

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