Reputation: 8350
I have this layout
Two SearchView widgets
One ListView
When I press the android back key the focus cycles between the filter box and the search box.
I want to go back to my previous activity, but I cannot do it, since the back key only goes from one box to the other.
How can I get the back key press event ?
this code does not even get called
@Override
public void onBackPressed() {
setResult(Activity.RESULT_CANCELED);
super.onBackPressed();
}
Upvotes: 2
Views: 805
Reputation: 8350
The problem is definitely with the SearchView widgets and its handling of the focus/backkey.
I changed the SearchViews to EditTexts
I changed the QueryTextListeners to TextWatchers
and... voila!
Now the onBackPressed() is called and the activity can return to its previous caller.
Upvotes: 1
Reputation: 19790
I always had trouble with the onBackPressed
. You should try to use onKeyDown
or onKeyUp
.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
return true;
}
return super.onKeyDown(keyCode, event);
}
Upvotes: 0
Reputation: 967
You can override activity's onBackPressed() method to do whatever job you wanna do on back key pressed like
you can call previous activity on back key press...like,
@override
public void onBackPressed ()
{
Intent intent = new Intent(CurrentActivity.this, PreviousActivity.class);
startActivity(intent);
finish();
}
Upvotes: 0