Skip to content

Commit fd98534

Browse files
authored
Fix webview Back button behaviour (#2636)
1 parent b3e8c03 commit fd98534

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

pythonforandroid/bootstraps/service_only/build/src/main/java/org/kivy/android/PythonActivity.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,22 @@ public void loadLibraries() {
177177
new File(getApplicationInfo().nativeLibraryDir));
178178
}
179179

180-
long lastBackClick = SystemClock.elapsedRealtime();
180+
long lastBackClick = 0;
181181
@Override
182182
public boolean onKeyDown(int keyCode, KeyEvent event) {
183-
// If it wasn't the Back key or there's no web page history, bubble up to the default
184-
// system behavior (probably exit the activity)
185-
if (SystemClock.elapsedRealtime() - lastBackClick > 2000){
183+
// Check if the key event was the Back button
184+
if (keyCode == KeyEvent.KEYCODE_BACK) {
185+
// If there's no web page history, bubble up to the default
186+
// system behavior (probably exit the activity)
187+
if (SystemClock.elapsedRealtime() - lastBackClick > 2000){
188+
lastBackClick = SystemClock.elapsedRealtime();
189+
Toast.makeText(this, "Tap again to close the app", Toast.LENGTH_LONG).show();
190+
return true;
191+
}
192+
186193
lastBackClick = SystemClock.elapsedRealtime();
187-
Toast.makeText(this, "Click again to close the app",
188-
Toast.LENGTH_LONG).show();
189-
return true;
190194
}
191195

192-
lastBackClick = SystemClock.elapsedRealtime();
193196
return super.onKeyDown(keyCode, event);
194197
}
195198

pythonforandroid/bootstraps/webview/build/src/main/java/org/kivy/android/PythonActivity.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import android.widget.AbsoluteLayout;
3333
import android.view.ViewGroup.LayoutParams;
3434

35+
import android.webkit.WebBackForwardList;
3536
import android.webkit.WebViewClient;
3637
import android.webkit.WebView;
3738
import android.webkit.CookieManager;
@@ -269,24 +270,30 @@ public static ViewGroup getLayout() {
269270
return mLayout;
270271
}
271272

272-
long lastBackClick = SystemClock.elapsedRealtime();
273+
long lastBackClick = 0;
273274
@Override
274275
public boolean onKeyDown(int keyCode, KeyEvent event) {
275-
// Check if the key event was the Back button and if there's history
276-
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
277-
mWebView.goBack();
278-
return true;
279-
}
280-
// If it wasn't the Back key or there's no web page history, bubble up to the default
281-
// system behavior (probably exit the activity)
282-
if (SystemClock.elapsedRealtime() - lastBackClick > 2000){
276+
// Check if the key event was the Back button
277+
if (keyCode == KeyEvent.KEYCODE_BACK) {
278+
// Go back if there is web page history behind,
279+
// but not to the start preloader
280+
WebBackForwardList webViewBackForwardList = mWebView.copyBackForwardList();
281+
if (webViewBackForwardList.getCurrentIndex() > 1) {
282+
mWebView.goBack();
283+
return true;
284+
}
285+
286+
// If there's no web page history, bubble up to the default
287+
// system behavior (probably exit the activity)
288+
if (SystemClock.elapsedRealtime() - lastBackClick > 2000){
289+
lastBackClick = SystemClock.elapsedRealtime();
290+
Toast.makeText(this, "Tap again to close the app", Toast.LENGTH_LONG).show();
291+
return true;
292+
}
293+
283294
lastBackClick = SystemClock.elapsedRealtime();
284-
Toast.makeText(this, "Click again to close the app",
285-
Toast.LENGTH_LONG).show();
286-
return true;
287295
}
288296

289-
lastBackClick = SystemClock.elapsedRealtime();
290297
return super.onKeyDown(keyCode, event);
291298
}
292299

0 commit comments

Comments
 (0)