Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion mobilewebview/android/src/org/mobilewebview/MobileWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,18 @@ public MobileWebView(Context context, long nativePtr, View rootView) {

private void initializeWebViewOnMainThread(Context context) {
try {
mWebView = new WebView(context);
mWebView = new WebView(context) {
@Override
public boolean dispatchKeyEvent(android.view.KeyEvent event) {
if (event.getKeyCode() == android.view.KeyEvent.KEYCODE_BACK) {
if (event.getAction() == android.view.KeyEvent.ACTION_UP) {
withNativePtr(MobileWebView.this::nativeOnBackRequested);
}
return true;
}
Comment on lines +88 to +93
return super.dispatchKeyEvent(event);
}
Comment on lines +87 to +95
};
setupWebView();
} catch (RuntimeException e) {
Log.e(TAG, "Failed to initialize WebView on main thread", e);
Expand Down Expand Up @@ -410,6 +421,12 @@ public void setVisible(boolean visible) {
runOnMainThread(() -> {
if (mWebView == null) return;
mWebView.setVisibility(visible ? View.VISIBLE : View.GONE);
// Grab focus when shown so the WebView receives key events
// (notably KEYCODE_BACK). Without focus, dispatchKeyEvent is not
// called and Back is never forwarded to QML.
if (visible) {
mWebView.requestFocus();
}
});
}

Expand Down Expand Up @@ -684,6 +701,7 @@ private native void nativeOnWebMessageReceived(long nativePtr, String message,
private native void nativeOnJavaScriptResult(long nativePtr, String result, String error);
private native void nativeOnTitleChanged(long nativePtr, String title);
private native void nativeOnNavigationStateChanged(long nativePtr, boolean canGoBack, boolean canGoForward);
private native void nativeOnBackRequested(long nativePtr);
private native void nativeOnHistoryChanged(long nativePtr, String[] urls, String[] titles, int currentHistoryIndex);
private native void nativeOnNewWindowRequested(long nativePtr, String url, boolean userInitiated);
private native void nativeOnLoadProgressChanged(long nativePtr, int progress);
Expand Down
18 changes: 18 additions & 0 deletions mobilewebview/src/android/mobilewebviewbackend_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <QDebug>
#include <QQuickWindow>
#include <QKeyEvent>
#include <QCoreApplication>
#include <QFile>
#include <QGuiApplication>
#include <QJniObject>
Expand Down Expand Up @@ -988,6 +990,22 @@ Java_org_mobilewebview_MobileWebView_nativeOnNavigationStateChanged(JNIEnv *env,
}, Qt::QueuedConnection);
}

JNIEXPORT void JNICALL
Java_org_mobilewebview_MobileWebView_nativeOnBackRequested(JNIEnv *env, jobject obj, jlong nativePtr)
{
Q_UNUSED(env)
Q_UNUSED(obj)
if (nativePtr == 0) return;

AndroidWebViewPrivate *backend = reinterpret_cast<AndroidWebViewPrivate*>(nativePtr);
QMetaObject::invokeMethod(backend->q_ptr, [backend]() {
QQuickWindow *win = backend->q_ptr->window();
if (!win) return;
QCoreApplication::postEvent(win, new QKeyEvent(QEvent::KeyPress, Qt::Key_Back, Qt::NoModifier));
QCoreApplication::postEvent(win, new QKeyEvent(QEvent::KeyRelease, Qt::Key_Back, Qt::NoModifier));
}, Qt::QueuedConnection);
}

JNIEXPORT void JNICALL
Java_org_mobilewebview_MobileWebView_nativeOnHistoryChanged(JNIEnv *env, jobject obj,
jlong nativePtr, jobjectArray urls,
Expand Down
Loading