Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 793
noobnicks Avatar asked Mar 03 '12 12:03

noobnicks


People also ask

How do I stop Android from restarting activity when changing orientations?

Prevent Activity to recreated Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.

What happen to the Android application when the device screen orientation has been changed from portrait to landscape view?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them . Android does this so that your application can reload resources based on the new configuration.

How can we cope with screen orientation changes?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.

What happens when screen orientation changes in Android?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them . Android does this so that your application can reload resources based on the new configuration.


2 Answers

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.

like image 166
waqaslam Avatar answered Oct 18 '22 22:10

waqaslam


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.

like image 4
Urban Avatar answered Oct 18 '22 22:10

Urban