If you are using InputMethodManager.showSoftInput(), you can try passing in a ResultReceiver and implementing onReceiveResult() to handle RESULT_HIDDEN
I had the same problem but got around it by intercepting the back key press. In my case (HTC Desire, Android 2.2, Application API Level 4) it closes the Keyboard and immediately finishes the Activity. Don't know why this should not work for you too:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
/**
* Called when the activity has detected the user's press of the back key
*/
private void onBackPressed() {
Log.e(TAG, "back pressed");
finish();
}
Yes, it is completely possible to show and hide the keyboard and intercept the calls to the back button. It is a little extra effort as it has been mentioned there is no direct way to do this in the API. The key is to override boolean dispatchKeyEventPreIme(KeyEvent) within a layout. What we do is create our layout. I chose RelativeLayout since it was the base of my Activity.
Anyway, here is my override of the RelativeLayout.
package com.michaelhradek.superapp.utilities;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.RelativeLayout;
/**
* The root element in the search bar layout. This is a custom view just to
* override the handling of the back button.
*
*/
public class SearchLayout extends RelativeLayout {
private static final String TAG = "SearchLayout";
private static Activity mSearchActivity;;
public SearchLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SearchLayout(Context context) {
super(context);
}
public static void setSearchActivity(Activity searchActivity) {
mSearchActivity = searchActivity;
}
/**
* Overrides the handling of the back key to move back to the
* previous sources or dismiss the search dialog, instead of
* dismissing the input method.
*/
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
Log.d(TAG, "dispatchKeyEventPreIme(" + event + ")");
if (mSearchActivity != null &&
event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
KeyEvent.DispatcherState state = getKeyDispatcherState();
if (state != null) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getRepeatCount() == 0) {
state.startTracking(event, this);
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP
&& !event.isCanceled() && state.isTracking(event)) {
mSearchActivity.onBackPressed();
return true;
}
}
}
return super.dispatchKeyEventPreIme(event);
}
}
I found out, that overriding the dispatchKeyEventPreIme method of the Layout Class also works well. Just set your main Activity as an attribute and launch a predefined method.
public class LinearLayoutGradient extends LinearLayout {
MainActivity a;
public void setMainActivity(MainActivity a) {
this.a = a;
}
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
if (a != null) {
InputMethodManager imm = (InputMethodManager) a
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive() && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
a.launchMethod;
}
}
return super.dispatchKeyEventPreIme(event);
}
}
onKeyDown() and onBackPressed() doesn't work for this case. You have to use onKeyPreIme.
Initially, you have to create custom edit text that extends EditText. And then you have to implement onKeyPreIme method which controls KeyEvent.KEYCODE_BACK. After this, one back press enough for solve your problem. This solution works for me perfectly.
CustomEditText.java
public class CustomEditText extends EditText {
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// User has pressed Back key. So hide the keyboard
InputMethodManager mgr = (InputMethodManager)
getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(this.getWindowToken(), 0);
// TODO: Hide your view as you do it in your activity
}
return false;
}
I needed to know when the hardware or gesture back button was pressed while the keyboard was showing so I could react and show a button that had previously been hidden when any of the edit text views had received focus.
First declare the call back interface your require
interface KeyboardEventListener {
fun onKeyBoardDismissedIme()
}
Then create the custom view with the listener for pre ime key events
class KeyboardAwareConstraintLayout(context: Context, attrs: AttributeSet) :
ConstraintLayout(context, attrs) {
var listener: KeyboardEventListener? = null
override fun dispatchKeyEventPreIme(event: KeyEvent?): Boolean {
val imm: InputMethodManager =
context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
if (imm.isActive && event?.keyCode == KeyEvent.KEYCODE_BACK) {
listener?.onKeyBoardDismissedIme()
}
return super.dispatchKeyEventPreIme(event)
}
}
Next implement the callback interface in your activity or fragment and set on the keyboard aware layout
class MyFragment : Fragment, KeyboardEventListener {
// TODO: Setup some viewbinding to retrieve the view reference
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.keyBoardAwareLayout.listener = this
}
override fun onKeyBoardDismissedIme() {
// TODO: React to keyboard hidden with back button
}
}