Search code examples
androidandroid-orientation

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


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 disappears. How can I make it opened even when orientation changes at runtime?


Solution

  • 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.