onBackPressed()不起作用

时间:2015-07-06 11:11:51

标签: android button layout onbackpressed

在一个Activity课程中,我使用不同的布局。

通过禁用一个布局,我将转移到另一个布局。但是在按下后退按钮时我遇到了问题。当我处于第三种布局时,它将进入第一个布局。

这是我的代码:

我使用了3个按钮移动到另一个布局。

public void continueToDelivery(View view) {
    relativeLayoutLogin.setVisibility(view.GONE);
    relativeLayoutDelivery.setVisibility(view.VISIBLE);
    relativeLayoutPayment.setVisibility(view.GONE);  
}   

public void continueToPayment(View view) {
    relativeLayoutDelivery.setVisibility(view.GONE);
    relativeLayoutPayment.setVisibility(view.VISIBLE);
}

public void placeOrder(View view) {
    relativeLayoutPayment.setVisibility(view.GONE);
    relativeLayoutSuccess.setVisibility(view.VISIBLE);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
    {
        onBackPressed();
    }
    return super.onKeyDown(keyCode, event);
}

1 个答案:

答案 0 :(得分:1)

在您的活动中覆盖onBackPressed

 @Override
    public void onBackPressed() {
      if(isThirdLayoutVisible)
       //go to second layout
      else if(isSecondLayoutVisible)
      //go to first layout
     else 
     //exit from app
    }