tags:

views:

205

answers:

3

I'm implementing the onBackPressed() method in my activity. It's crucial to my app that I have this functionality. But, the control never enters this function. It enters onPause() instead, when I press the back button.

But the problem is I can't have the same logic in onPause() because when I call another activity, the current activity calls onPause() and I don't want it to execute what should be in onBackPressed().

Please help.

public void onBackPresed(){

   Log.d(TAG,"inside onBackPressed()");
       if(STATE == PREVIEW){

       } 
}
+3  A: 
public void onBackPresed(){

you typed one 's', it should be onBackPre*ss*ed()

Mathias Lin
thanks a lot!!!
Namratha
This is why you should always use @Override. :)
hackbod
yeah! that's right:)
Namratha
+1  A: 

This is how I do it.

public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
    Log.d(TAG,"Back key pressed");
    return true;
}
return super.onKeyDown(keyCode, event);
}
androidworkz
oh. Is this better than onBackPressed(). Does one methos have an advantage over the other?I think one advantage with this method, is when you're listening for key presses, you can use a switch case and include the back press too.
Namratha
A: 

Just a heads up, the Android developer's blog posted about overriding the back press and dealing with backwards compatibility here.

Josh