2

我有一个webView内部活动的布局。如果按下后退按钮,我希望该视图消失并且其他视图变得可见,我做了以下操作:

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && web.getVisibility()==View.VISIBLE) {
        restoreInitalState(); // set Visibility of Views
        APP_CONSTANTS.LOGIN_OP_CANCELLED(getApplicationContext()); // inform the user that the current operation was cancelled
    }
    return super.onKeyDown(keyCode, event);

}

它可以工作,但它在调用我的方法后立即完成 Activity,就像按下后退按钮 2 次一样。我需要保留当前的 ​​Activity 并调用我上面提到的方法。有什么建议么?

4

4 回答 4

3

你需要返回false。改变这个:

if (keyCode == KeyEvent.KEYCODE_BACK && web.getVisibility()==View.VISIBLE) {
    restoreInitalState(); // set Visibility of Views
    APP_CONSTANTS.LOGIN_OP_CANCELLED(getApplicationContext()); // inform the user that the current operation was cancelled
}

对此:

if (keyCode == KeyEvent.KEYCODE_BACK && web.getVisibility()==View.VISIBLE) {
    restoreInitalState(); // set Visibility of Views
    APP_CONSTANTS.LOGIN_OP_CANCELLED(getApplicationContext()); // inform the user that the current operation was cancelled
    return false;
}
于 2013-02-25T17:47:29.527 回答
1

我认为正确的方法是:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && web.getVisibility()==View.VISIBLE) {
        restoreInitalState(); // set Visibility of Views
        APP_CONSTANTS.LOGIN_OP_CANCELLED(getApplicationContext()); 
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
于 2013-02-25T17:49:33.623 回答
1

或者只是onBackPressed()在你的Activity类中使用该方法,这是覆盖它的最简单方法。

于 2013-02-25T17:51:35.353 回答
1

我认为您正在寻找的回调方法是onBackPressed.

But your current solution should work as well, you just need to return true inside your if-block otherwise the event will be propagated to another callback.

于 2013-02-25T17:54:38.503 回答