Question

Basically:

Is it possible to prevent specific window events from firing within JavaScript running inside a WebView? Specifically I want to prevent window.orientationchange events being fired.

(Alternatively is there any way to be absolutely certain I am inside a WebView and not in a normal Chrome/built-in browser window without overriding the User-Agent?)

In detail:

I've been tasked with embedding an existing web application inside a basic WebView container on Android. The web app does not support portrait configurations, or browsers resized into a portrait aspect-ratio.

To fix that, there is a window.orientationchange handler in the JavaScript to detect this and display an error message if necessary.

For user-experience, we have the following Activity XML to prevent the user changing the orientation:

<activity
    android:name="com.example.my-stackoverflow-app"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
    android:screenOrientation="landscape">

This works perfectly. The trouble is, orientationchange events get fired when pausing/resuming the Activity.

Since the Home Screen is portrait the window.orientationchange handler is detecting this before the Activity re-orients itself and is locking me out of the app.

Naturally enough, a setTimeout call fixes this, but there is a rather large sky-scraper outside my window which I will be visiting before resorting to a setTimeout fix.

Was it helpful?

Solution

If you can't modify the user agent of your webview app, you could load a javascript: URL from your WebView app that sets a flag to say you are running inside a WebView, then check that flag when the event fires in JS... you'd need to re-inject this JS every time a new page loads. You could also add a JavaScript interface object from your java; and test for it's presence to detect the WebView. Once you add a JS interface object, it will be maintained automatically across page loads, but be careful (see docs for warnings[1])

Out of curiosity, why are you unable to override the UA?

[1] http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top