How to open popup when orientation changes at runtime on Android?

I created a popup that contains a month view to select a date. When I change orientation, due to Android loading the action again, my popup disappears. How can I open it even when the orientation changes at runtime?

+7
source share
4 answers

include android:configChanges="orientation" in AndroidManifest.xml in the activity display window. Doing this tells the android that you are going to handle the orientation change yourself, and ultimately it will not ruin your activity and will not display a window.

This technique is good if you do not have different layouts for portrait and landscape mode. However, if you do this, you can still implement the custom layout by specifying an orientation mode, as shown 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"); } 

to preview, download, and install this sample application .

+5
source

Whenever a change in orientation occurs, Android destroys your activity (calls onDestroy() ) and then restarts it (calls onCreate() ).
Once your popup is up, set the flag popup_open=1 . Naturally, your pop-up window has a dismiss button. Set flag = 0 to the click handler of this button. You can then reopen the popup when the application restarts in the onRestoreInstanceState() method or in onCreate() . Here you check the box. If the flag is set to 1, display a pop-up window. Therefore, even if the orientation changed during the popup, onRestoreInstanceState() will know what to do based on the state of the flag. For additional reference: How to handle runtime changes .

+4
source

Add this property to your activity in manifest.xml

 android:configChanges="orientation|keyboard" 

and it must be done.

+1
source

Display PopupWindow as

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

allows unhandled exception when orientation changes

0
source

All Articles