我在Activity的布局中有一个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并调用上面提到的方法。有什么建议吗?
答案 0 :(得分: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;
}
答案 1 :(得分: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);
}
答案 2 :(得分:1)
或者只是在你onBackPressed()
类中使用方法Activity
,这是覆盖它的最简单方法。
答案 3 :(得分:1)
我认为您正在寻找的回调方法是onBackPressed
。
但是你当前的解决方案也应该工作,你只需要在if-block中返回true,否则事件将传播到另一个回调。