The desired behaviour is this:
-
On rotation the layout is reconstructed – in order to fit new screen size. Reconstruction is done with JQuery and maths – not CSS.
-
The scripts state must keep the same, also running timers, etc. These should continue like nothing happened.
In order to achieve this I made this:
-
Added android:configChanges=”orientation|keyboardHidden” to all activities in the manifest, to handle orientation change myself (note: I’m not targeting certain API, so I suppose it defaults to API 1. This means that add screenSize to configChanges is not necessary).
-
Added a listener for orientationchange:
window.onorientationchange = function() {
var orientation = window.orientation;
switch(orientation) {
case 0:
updateScaling();
break;case 90: updateScaling(); break; case -90: updateScaling(); break; } }
updateScaling only adjusts the layout.
And this works sometimes, but sometimes the device crashes (emulator as well as real device). On the emulator’s log I see this warning:
WARN/ActivityManager(73): Launch timeout has expired, giving up wake lock!
I made research and found add a longer loadUrlTimeoutValue in the activity could help, and well, I added super.setIntegerProperty(“loadUrlTimeoutValue”, 60000); to the main activity but as expected doesn’t help either.
I found many people complaining about rotation problems with PhoneGap. Is maybe the Framework just buggy for this aspect? I tested also with Apple devices, and there I was not even possible to catch the rotating event – the iPhone emulator didn’t even change the orientation, the iPad changed it but the event handler is not called and the layout keeps the same size. I tried using a workaround I found attaching a listener to document. But it didn’t help either.
Is there a fully reliable way to reconstruct the layout when rotating the device? If not, can I disable rotation after the app being initialized with a certain one (that means, I don’t want to fix to portrait or landscape – I want the application disables rotation changes after it was initialized using the current one). Is there a reliable way to do this?
Thanks
I’m not quite sure if this fixed the crash problem, or something else I did “on the way”.
But I added a listener which works reliably – at least on Android:
It’s always called and passes correct width. As far as I remember it also works with screen.width
In updateScaling I calculate a scalingFactor and adjust my elements.
I removed window.onorientationchange.
Note: It might be that at the point of time of the crashes I had android:configChanges=”keyboardHidden” instead of android:configChanges=”orientation|keyboardHidden” in the manifest. AFAIK this means the system reconstruct the activity, this could explain the timeout. But in that case I don’t know why super.setIntegerProperty(“loadUrlTimeoutValue”, 60000); didn’t help.
Conclusion
$(window).resize in combination with android:configChanges=”orientation|keyboardHidden” is working for me.