Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onBackPressed method is not working properly

In my app there is one EditText. I have called keypad show on my activity for this EditText. keypad showing & working fine.

Now in currently, I have to press back button two time, one for hiding keypad & another for performing some task(like data saving to DB).I don't want to press back button two times.

please guide me, or suggestion

here some from onBackPressed()

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

    hideKeypad();

    //saving EditText data to db.
}

code form hideKeypad()

private void hideKeypad() {
       InputMethodManager imm = (InputMethodManager) 
        getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(edittext1.getWindowToken(), 0);
}
like image 722
PSK Avatar asked Mar 20 '13 09:03

PSK


2 Answers

Use this method it is similar what you already done but I little modified it . Register listener of your main layout and pass its object as param so that when you click out side of edit text i,e. main layout keypad will gone.

/** 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);    
    }
like image 166
Akhilesh Mani Avatar answered Oct 18 '22 10:10

Akhilesh Mani


Pressing back to close the keyboard is a standard function of the UI. Why would you want to change this?

Users who are used to using a soft-keyboard on Android devices will be used to pressing back once to close the keyboard, then once more to move back in the application.

If you make the back button move back in the application while the keyboard is displayed, it makes navigation different to standard Android navigation, and many users may get frustrated with it.

Besides that, your onBackPressed is not being called because when the keyboard is displayed, the onBackPressed for the keyboard is run - which, as we've all seen - is what hides the keyboard - and not the onBackPressed for your application.

Mr.Me's answer is the way to do it if you do want to go that way, but I wouldn't recommend it.

like image 32
Jamie - Fenrir Digital Ltd Avatar answered Oct 18 '22 09:10

Jamie - Fenrir Digital Ltd