Reputation: 7191
I have onPause in activity and works great. This is code
@Override
protected void onPause() {
super.onPause();
Intent intent;
intent = new Intent(Games.this, PauseScreen.class);
startActivity(intent);
}
But I have a problem. When I push the back button onPause is calling and start new activity. I want to onPause works always except when I click back button on my phone. How I can do this? I think that may be if(backbutton was click){onPause is not working}else{onPause working}
but I don't know how implement this solution. Or maybe you have better idea?
Upvotes: 1
Views: 1515
Reputation: 7087
@user1302569
Here are step you can follow to achieve your desire behavior in Application..
STEP 1: Overide this back Key method in your Activty.
@Override
public void onBackPressed() {
//Here you get Back Key Press So make boolean false
no_back_key=false;
super.onBackPressed();
}
STEP 2: Take One boolean variable like below in your Activity Class.
public boolean no_back_key=true;
STEP 3: in your OnPause Method do some thing like Below
@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);
}
STEP 4: in Your Activity's OnResume Make sure this also.
@Override
protected void onResume() {
no_back_key=true;
super.onResume();
}
}
Regarding Back Key Event in Android ,in Developer site you can refer this http://android-developers.blogspot.in/2009/12/back-and-other-hard-keys-three-stories.html
Upvotes: 2
Reputation: 48272
Try overriding onBackPressed()
setting a flag in there and checking that flag in your onPause()
(It is strange though to start another activity from onPause but maybe you have good reasons to do so I can't think of such a scenario though...)
Upvotes: 0
Reputation: 16191
Implement the following method-
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Do something (OR NOTHING)
}
return true;
}
Remember to return true
since it means that, you've handled on back key press and system does not need to handle it.
This is a good substitute for onBackPressed()
which may or may not work for you.
Upvotes: -1