使用 JavaScript/jQuery 检测 Android 后退按钮 [英] Use JavaScript/jQuery to detect the Android back button

查看:32
本文介绍了使用 JavaScript/jQuery 检测 Android 后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我想做一个函数来检测 Android 后退按钮是否被按下并在上面做一些事情.

Currently I want to make a function to detect the Android back button is pressed and do some stuff on it.

  1. 用户打开应用程序,然后单击一个按钮.此按钮可让用户打开网站.

  1. The user opens the app, and clicks a button. This button lets the user to open a website.

用户填写第2页信息,想返回上一页,点击Android手机的返回键.

The user fills in the information on page 2, and he/she wants to go back the previous page, and he/she clicks the back button on the Android phone.

后退按钮可帮助用户关闭网站并返回到我的应用程序,而不是返回到第 1 页.

The back button helps the user close the website and go back to my apps instead of back to page 1.

如何使用 jQuery 或 JavaScript 来检查用户按下的后退按钮?这样我就可以防止后退按钮关闭网站,但重定向到另一个页面.

How can I do this with jQuery or JavaScript to check the back button pressed by user? So that I can prevent the back button close the website, but redirect to another page.

有没有办法检测浏览器的Android后退按钮事件?我想覆盖Android后退按钮的默认设置,并在里面添加一些代码.

PS:不使用 Java.

  • 顶部的后退按钮(浏览器后退按钮)可以返回上一页.
  • 底部的后退按钮(手机后退按钮)帮助我关闭网站.
  • 我想让底部的后退按钮也返回上一页.

看到这个.

推荐答案

您需要覆盖 WebView Activity 上的 onBackPressed 并检查 webview 是否可以返回.

You need to override the onBackPressed on WebView Activity and check if webview can go back or not.

@Overide
public void onBackPressed() {
    if(webview.canGoBack())
        webview.goBack();
    else
        super.onBackPressed();
}

Meta:当给出这个答案时,这个问题并不特定于仅限 JavaScript"不使用 Java 编码" 等.. 即仅由后端处理.这个答案是严格针对 Android 端的,而不是针对后端端的.如果来自 Android 的人需要这个答案,我会保留这个答案.

Meta: When this answer was given, the question was not specific for "JavaScript only" and "not coding in Java", etc.. i.e. handle only by the backend. This answer is strictly for the Android side, not for the backend side. I am keeping this answer if someone from Android need this answer.

这篇关于使用 JavaScript/jQuery 检测 Android 后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆