1

I want to handle back press like whatsapp where while typing if user presses back the keyboard is dismissed and if the user presses back again ,we go to chats tab.In my activity I want that when the user presses back while typing the keyboard dismisses and some UI elements are handled.I tried handling that in the onBackPressed() method , but it isn't working and the activity is killed.

     edittext.setOnEditorActionListener (
                    new EditText.OnEditorActionListener () {
                        @Override
                        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                            if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                                    actionId == EditorInfo.IME_ACTION_DONE ||
                                    event != null &&
                                            event.getAction () == KeyEvent.ACTION_DOWN &&
                                            event.getKeyCode () == KeyEvent.KEYCODE_ENTER) {
                                if (event == null || !event.isShiftPressed ()) {
                                    // the user is done typing.

                                    View view1 = getCurrentFocus ();
                                    imm.hideSoftInputFromWindow (view1.getWindowToken (), 0);

                                    Log.e (TAG, "TYPING DONE ");
                                    result = tv_result.getText ().toString();

                                    return true; // consume.
                                }
                            }
                            return false; // pass on to other listeners.
                        }
                    }
            );

the onBackPressed() code

     @Override
    public void onBackPressed() {

    tv_result.setText("Some text");

}
Neha
  • 389
  • 4
  • 8
  • 24

2 Answers2

2

Since you haven't provided any code, and I can't comment yet, I will assume you're letting the onBackPressed do what it per default does, meaning that in your onBackPressed() method you have super.onBackPressed(); , removing that line should solve the issue

Edit Since you've updated your question with code, whatever is written after super.onBackPressed() will not be seen visually, as super.onBackPressed() goes back to the previous activity. Remove super.onBackPressed(), and handle the second back press with and if else statement

Omar Hezi
  • 155
  • 8
  • I removed the `super.onBackPressed();` ,but still the problem is we press back to dismiss the keyboard and then again press back and here the UI is changed .I want when the keyboard is dismissed on back pressed , the Ui should be handled here – Neha Apr 12 '18 at 12:51
  • I added the if statement and handled it .@Omar thanks a lot for your answer. – Neha Apr 12 '18 at 13:03
0

If it's on activity, onBackPressed() function do the following,

// hide virtual keyboard
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(m_txtSearchText.getWindowToken(), 
                                      InputMethodManager.RESULT_UNCHANGED_SHOWN);
Subin Babu
  • 1,515
  • 2
  • 24
  • 50
  • the keyboard is dismissed when the user presses back , I want to do is when the keyboard is dismissed by user , some UI elements must be handled – Neha Apr 12 '18 at 12:39
  • @Neha https://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android – Subin Babu Apr 12 '18 at 12:44