我在 Activity 中有 onPause,效果很好。这是代码
@Override
protected void onPause() {
super.onPause();
Intent intent;
intent = new Intent(Games.this, PauseScreen.class);
startActivity(intent);
}
但是我有一个问题。当我按下后退按钮时,onPause 正在调用并开始新的 Activity 。我希望 onPause 始终有效,除非我单击手机上的后退按钮。我该怎么做?我认为可能是 if(backbutton was click){onPause is not working}else{onPause working}
但我不知道如何实现此解决方案。或者您有更好的主意?
最佳答案
@user1302569
您可以按照以下步骤在应用程序中实现您想要的行为..
第 1 步:在您的 Activty 中覆盖此返回键方法。
@Override
public void onBackPressed() {
//Here you get Back Key Press So make boolean false
no_back_key=false;
super.onBackPressed();
}
第 2 步:在您的 Activity 类中采用如下所示的一个 bool 变量。
public boolean no_back_key=true;
第 3 步:在您的 OnPause 方法中执行如下操作
@Override
protected void onPause() {
super.onPause();
Intent intent;
//Only this boolean will become false when we get Back Key Press as you Said in Your Question
if(no_back_key){
intent = new Intent(Games.this, PauseScreen.class);
startActivity(intent);
}
第 4 步:在您的 Activity 的 OnResume 中确保这一点。
@Override
protected void onResume() {
no_back_key=true;
super.onResume();
}
关于 Android 中的返回键事件,在 Developer site 你可以引用这个 http://android-developers.blogspot.in/2009/12/back-and-other-hard-keys-three-stories.html
关于android - OnPause 和返回按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9941350/