Java – how to use onbackpressed () to see the finish () activity with the softkeyboard activity
I have an activity. The whole screen is dedicated to sending a message. The upper part is an EditText, while the lower part is always visible. When I press back, the soft keyboard will be hidden, and I must press back again to exit the activity
The behavior I'm trying to get is to complete the activity immediately when I press the back button instead of hiding the keyboard. For example, when writing a new tweet, you can find this behavior in the twitter application
I tried to override the onbackpressed() function, but it didn't seem to be called when the keyboard was visible
@Override
public void onBackPressed() {
finish();
}
Any help will be appreciated!
@R_ 403_ 1911@:
Therefore, after trying many things, the method here works:
Subclass EditText and override onkeypreime() function to send callback. This is the code of subclass:
OnKeyPreImeListener onKeyPreImeListener;
public void setOnKeyPreImeListener(OnKeyPreImeListener onKeyPreImeListener) {
this.onKeyPreImeListener = onKeyPreImeListener;
}
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
if(onKeyPreImeListener != null)
onKeyPreImeListener.onBackPressed();
Log.d(TAG, "HIDING KEYBOARD");
return false;
}
return super.dispatchKeyEvent(event);
}
public interface OnKeyPreImeListener {
void onBackPressed();
}
Then in your activity for each textview:
EditTextGraphee.OnKeyPreImeListener onKeyPreImeListener =
new EditTextGraphee.OnKeyPreImeListener() {
@Override
public void onBackPressed() {
Log.d(TAG, "CALL BACK RECEIVED");
MyActivity.this.onBackPressed();
}
};
editText.setOnKeyPreImeListener(onKeyPreImeListener);