0

I have one edittext in my activity. when user touches on that edittext, keyboard is open. But in all HTC device, after opening keyboard, and when user press back button, instead of hiding only keyboard, my current activity is finished and showing previously activity. How to resolve this problem? in all other samsung mobile, this works fine. But not in HTC devices.

Mihir Shah
  • 1,799
  • 3
  • 29
  • 49
  • Its deivce dependent.. may be need to add extra code to control HTC settings regarding hiding keyboard. – Kiran Mar 21 '13 at 04:31

3 Answers3

0

You may be pressing the back button twice, because I have done the same thing but have not encountered a similar problem. I've tested my code on both Samsung and HTC devices.

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
Carbon
  • 133
  • 1
  • 4
  • 21
0

Well this might just work for you. You can check if the keyboard is open on onBackPressed event like :

public void onBackPressed() {
        final View activityRootView = findViewById(R.id.activityRoot);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
                if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                   InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
                }
             } else {
                 super.onBackPressed();
             }
        });
}

then you can dismiss the keyboard first like in above code and then finally when the back is pressed again you can just go back by using super.onBackPressed();

Sanober Malik
  • 2,765
  • 23
  • 30
0

Frankly, I'm of the mind to say leave it alone and let the platform handle it the way the user will expect it to on their device. However, I recently ran into an issue with something similar recently, and while it's not ideal, it does allow you to intercept the back button before it reaches the keyboard, and handle it however you want.

First, whatever the root ViewGroup your layout uses, override it in a manner similar to this:

IMEInterceptLayout extends LinearLayout {
    private OnBackPressedPreIMEListener listener;

    public IMEInterceptLayout(Context c) { super(c); }
    public IMEInterceptLayout(Context c, AttributeSet attrs) { super(c, attrs); }
    public IMEInterceptLayout(Context c, AttributeSet attrs, int style) { super(c, attrs, style); }

    @Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
        switch(event.getKeyCode()) {
            case KeyEvent.KEYCODE_BACK:
                fireOnBackPressedPreIME();
                return true;
            default:
                return super.dispatchKeyEventPreIme(event);
        }
    }

    public void setOnBackPressedPreIMEListener(OnBackPressedPreIMEListener listener) {
        this.listener = listener;
    }

    private void fireOnBackPressedPreIME() {
        if(listener != null) listener.onBackPressedPreIME();
    }

    public interface OnBackPressedPreIMEListener {
        public void onBackPressedPreIME();
    }
}

e.g. If you're using a RelativeLayout, extend that instead of LinearLayout. Use this custom view in your layout instead of the stock Android version:

<LinearLayout
    android:id="@+id/my_root_layout"

becomes

<com.my.packagename.IMEInterceptLayout
    android:id="@+id/my_root_layout"

Then, in your onCreate() after setting the content view to this layout, get a reference to this ViewGroup:

IMEInterceptLayout layout = (IMEInterceptLayout)findViewById(R.id.my_root_layout);
layout.setOnBackPressedPreIMEListener(new OnBackPressedPreIMEListener() {
    @Override
    public void onBackPressedPreIME() {
        InputMethodManager imm = (InputMethodManager)MyActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
        View focusedView = MyActivity.this.getCurrentFocus();
        if(focusedView != null) 
            imm.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
    }
}

Obviously replacing MyActivity with the name of your actual Activity. This will allow you to dismiss the keyboard yourself instead of relying on the system to do it for you. It's a relatively large amount of work for the result, but it's reliable.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274