|
32 | 32 | import android.widget.AbsoluteLayout; |
33 | 33 | import android.view.ViewGroup.LayoutParams; |
34 | 34 |
|
| 35 | +import android.webkit.WebBackForwardList; |
35 | 36 | import android.webkit.WebViewClient; |
36 | 37 | import android.webkit.WebView; |
37 | 38 | import android.webkit.CookieManager; |
@@ -269,24 +270,30 @@ public static ViewGroup getLayout() { |
269 | 270 | return mLayout; |
270 | 271 | } |
271 | 272 |
|
272 | | - long lastBackClick = SystemClock.elapsedRealtime(); |
| 273 | + long lastBackClick = 0; |
273 | 274 | @Override |
274 | 275 | 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 | + |
283 | 294 | lastBackClick = SystemClock.elapsedRealtime(); |
284 | | - Toast.makeText(this, "Click again to close the app", |
285 | | - Toast.LENGTH_LONG).show(); |
286 | | - return true; |
287 | 295 | } |
288 | 296 |
|
289 | | - lastBackClick = SystemClock.elapsedRealtime(); |
290 | 297 | return super.onKeyDown(keyCode, event); |
291 | 298 | } |
292 | 299 |
|
|
0 commit comments