Reputation: 21899
I want to use onBackPressed() method and still want to provide support for Android SDK before 2.0. onBackPressed() is introduced in Android SDK 2.0. but how to do ?
Upvotes: 3
Views: 5153
Reputation: 21899
Answer ---> http://apachejava.blogspot.com/2011/01/backward-compatibility-using.html
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
&& keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
// Take care of calling this method on earlier versions of
// the platform where it doesn't exist.
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onBackPressed() {
// This will be called either automatically for you on 2.0
// or later, or by the code above on earlier versions of the
// platform.
return;
}
Upvotes: 1
Reputation: 1201
You may capture a key event and check for the back key. On your activity:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
And write the goBack method to go where you need.
More at: Android - onBackPressed() not working
Upvotes: 4
Reputation: 3924
Using onKeyDown;
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Your Code Here
return true;
}
return super.onKeyDown(keyCode, event);
}
Upvotes: 8