OnBackPressed method does not work correctly

There is one in my application EditText. I called the keyboard display in my activity for this EditText. The keyboard shows and works fine.

Now, at present, I have to press the button twice two times, one to hide the keyboard and the other to perform some task (for example, saving data to the database). I do not want to press the return button twice.

please help me or suggestion

here are some of onBackPressed()

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    hideKeypad();

    //saving EditText data to db.
}

the code hideKeypad()

private void hideKeypad() {
       InputMethodManager imm = (InputMethodManager) 
        getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(edittext1.getWindowToken(), 0);
}
+5
source share
3 answers

, , , . param, i, e. .

/** Close Keypad on touch.
 * @param view on which click happens. */

 public void closeKeyPad(View view)
    {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);    
    }
+1

, BackPressed. . , .

:

    edit.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.getAction() == KeyEvent.ACTION_DOWN &&
        event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
    if (!event.isShiftPressed()) {
       // the user is done typing.  finish the activity
       finish();
       return true; // consume.
    }                
}
return false; // pass on to other listeners. 
}
});
0

. ?

, Android, , , , .

If you return the back button in the application while the keyboard is displayed, this makes the navigation different from the standard Android navigation, and many users may be upset with it.

Also, yours is onBackPressednot called, because when the keyboard is displayed, it launches onBackPressedfor the keyboard - which, as we all saw, is what the keyboard hides - and not onBackPressed for your application.

Mr.Me's answer is a way to do this if you want to go this way, but I would not recommend it.

0
source

All Articles