Skip to content

Fix webview Back button behaviour #2636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 23, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,22 @@ public void loadLibraries() {
new File(getApplicationInfo().nativeLibraryDir));
}

long lastBackClick = SystemClock.elapsedRealtime();
long lastBackClick = 0;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
if (SystemClock.elapsedRealtime() - lastBackClick > 2000){
// Check if the key event was the Back button
if (keyCode == KeyEvent.KEYCODE_BACK) {
// If there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
if (SystemClock.elapsedRealtime() - lastBackClick > 2000){
lastBackClick = SystemClock.elapsedRealtime();
Toast.makeText(this, "Tap again to close the app", Toast.LENGTH_LONG).show();
return true;
}

lastBackClick = SystemClock.elapsedRealtime();
Toast.makeText(this, "Click again to close the app",
Toast.LENGTH_LONG).show();
return true;
}

lastBackClick = SystemClock.elapsedRealtime();
return super.onKeyDown(keyCode, event);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import android.widget.AbsoluteLayout;
import android.view.ViewGroup.LayoutParams;

import android.webkit.WebBackForwardList;
import android.webkit.WebViewClient;
import android.webkit.WebView;
import android.webkit.CookieManager;
Expand Down Expand Up @@ -269,24 +270,30 @@ public static ViewGroup getLayout() {
return mLayout;
}

long lastBackClick = SystemClock.elapsedRealtime();
long lastBackClick = 0;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
if (SystemClock.elapsedRealtime() - lastBackClick > 2000){
// Check if the key event was the Back button
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Go back if there is web page history behind,
// but not to the start preloader
WebBackForwardList webViewBackForwardList = mWebView.copyBackForwardList();
if (webViewBackForwardList.getCurrentIndex() > 1) {
mWebView.goBack();
return true;
}

// If there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
if (SystemClock.elapsedRealtime() - lastBackClick > 2000){
lastBackClick = SystemClock.elapsedRealtime();
Toast.makeText(this, "Tap again to close the app", Toast.LENGTH_LONG).show();
return true;
}

lastBackClick = SystemClock.elapsedRealtime();
Toast.makeText(this, "Click again to close the app",
Toast.LENGTH_LONG).show();
return true;
}

lastBackClick = SystemClock.elapsedRealtime();
return super.onKeyDown(keyCode, event);
}

Expand Down