Android 安卓WebView套壳H5网页 手机返回键问题(过滤二级页面,返回键相应给WebView)

1.首先 重写 onKeyDown方法  添加如下代码:

/**
 * 按键响应,在WebView中查看网页时,检查是否有可以前进的历史记录。
 */
@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) && webView.canGoBack()) {

        // 返回键退回
        webView.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)
    return super.onKeyDown(keyCode, event);
}

2.代码写到这里会出现一个问题,多次点击底部导航按钮 再点击返回键 会相应WebView的GoBack,导致在首页 底部导航反向跳而不退出应用 

解决办法,

   a.  声明成员变量 :

   private boolean isMainAvtivity = true;
b.onKeyDown方法中 改写成如下 意思是如果
isMainAvtivity是true的话 出走退出应用的代码 如果是false的话 相应webview的GoBack事件
/**
 * 按键响应,在WebView中查看网页时,检查是否有可以前进的历史记录。
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if (isMainAvtivity) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - 
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值