在Android中更改主页按钮单击时的布尔值

时间:2012-08-10 07:58:02

标签: android button

我正在尝试仅在主页按钮按下时更改布尔值。谁能告诉我这个解决方案?

2 个答案:

答案 0 :(得分:1)

查看onUserLeaveHint()

  

当活动即将到来时,被称为活动生命周期的一部分   作为用户选择的结果进入后台。例如,何时   用户按下Home键,将调用onUserLeaveHint(),但是   当来电通话导致通话活动时   自动带到前台,onUserLeaveHint()不会   呼吁活动被打断。在调用它的情况下,   在活动的onPause()回调之前调用此方法。

onUserLeaveHint()中的布尔变量设为true:

 @Override
    public void onUserLeaveHint() {
                super.onUserLeaveHint();
                //make Boolean true here because this method first  
                //called when user press home key
            }

答案 1 :(得分:1)

您需要覆盖附加到窗口方法。 请检查以下代码。

 @Override
 public void onAttachedToWindow() {
 this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
 super.onAttachedToWindow();
 //set your boolean value here
 }