Rajat kumar
Rajat kumar

Reputation: 884

Keyboard remain opens when Toolbar back arrow is pressed

I am following below code:-

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle arrow click here
        if (item.getItemId() == android.R.id.home) {

            finish();
            overridePendingTransition(R.transition.right_in, R.transition.right_out);


        }
        return super.onOptionsItemSelected(item);
    }

In this when my keyboard is open and I press my toolbar back arrrow ,the keyboard remain open and activity got finish. I have tried forcefully hiding keyboard in on pause() by callling below method but doesn't look good while transition :-

public static void hideKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        //Find the currently focused view, so we can grab the correct window token from it.


        View view = activity.getCurrentFocus();
        //If no view currently has focus, create a new one, just so we can grab a window token from it
        if (view == null) {
            view = new View(activity);
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

    }

Upvotes: 2

Views: 687

Answers (2)

Mesabloo
Mesabloo

Reputation: 337

Why are you getting android home button with onOptionsItemSelected() ? Just do like above :

toolbar.setNavigationOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            main = new Intent(SettingActivity.this, MainActivity.class);
            main.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
            finish();

            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);

            startActivity(main);
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    });

Hope it will help you, Darkball60

Upvotes: 0

Adley
Adley

Reputation: 541

Try to put in your toolbar back button this code:

//Hide keyboard when button was clicked.
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);

like this:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle arrow click here
        if (item.getItemId() == android.R.id.home) {

            finish();
            overridePendingTransition(R.transition.right_in, R.transition.right_out);


        }
    //Hide keyboard when button was clicked.
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
        return super.onOptionsItemSelected(item);
    }

Upvotes: 2

Related Questions