Action on the back button when the keyboard is active

2

I'm trying to use the back button action to pop up a menu in my application, with the onKeyUp method. As soon as the back key was clicked, the menu should immediately appear.

In my application I work with tabHost, there are 3 screens, in the first two there is a normal menu, in the third it is a search screen, and when I go to it, the keyboard automatically goes up, and the menu disappears, because at the moment it is unnecessary.

My problem is being to show this menu again after clicking the back button, it works in a way, but only appears on the second click of the back button. That is, the first click is only for downloading the keyboard, and the second that appears the menu. But I want the first click to lower the keyboard and show the menu, and I can not. Follow the methods, all in MainActivity.

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {

    if (event != null && KeyEvent.KEYCODE_BACK == event.getKeyCode() || keyCode == KeyEvent.KEYCODE_BACK) {
        Log.i("BOTAO VOLTAR", "BOTÃO VOLTAR CLICADO!!!");
        hideShowTabs(false);
    }
    return false;
}

public void hideShowTabs(boolean option) {

        if (option)
            tabHost.getTabWidget().setVisibility(View.GONE);
        else
            tabHost.getTabWidget().setVisibility(View.VISIBLE);
}

Can anyone tell me why it does not work on the first click ?? How would it work? I have two options to do: identify when the keyboard is downloaded, or when the back key is clicked, to display the menu. I accept suggestions, thank you jpa.

    
asked by anonymous 23.02.2016 / 22:00

1 answer

0

This is possible, but unfortunately the Android SDK does not provide a simple way to resolve this issue. Before attempting to deploy the solution I would strongly recommend that you really need to intercept onBackPressed() when the keyboard is present.

My answer is based this enlightening contribution

  • create a custom version of View where you need to recover onKeyPressed
  • Subscribe% with%
  • Process the event your way. In the snipped below I have chosen to process the event on the subscribed object and return a result for the custom view to proceed with its default actions.
  • Create an interface to receive the dispatchKeyEventPreIme(KeyEvent event) event in View
  • Enclose your Activity (or other object) to receive the event
  • OBS 1 - You need to understand the least about creating custom views.

    OBS 2 - I found the question interesting and I intend to give a tutorial on the subject in more detail. I should do it this week. Then post the link here.

    OBS 3 - You can check out the my gitHub a complete implementation

    ;

     */ Registre o callback para receber o evento
     */
    public void setListener(BackPressed callback){
        mCallback = callback;
    }
    
    /**
     * Subscreva o evento <code>dispatchKeyEventPreIme</code>.
     * Intercepte o tipo de evento <code>KeyEvent.KEYCODE_BACK</code>
     * e faça o que quiser com ele.
     */
    @Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
        Log.d(TAG, "dispatchKeyEventPreIme(" + event + ")");
        if ( mCallback != null ) {
            // O View verifica o tipo de evento
            // dá a oportunidade do objeto inscrito
            // processar o evento e anula a ação padrão
            // em caso de retorno positivo
            if ( event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                KeyEvent.DispatcherState state = getKeyDispatcherState();
                if (state != null) {
                    if (event.getAction() == KeyEvent.ACTION_DOWN
                            && event.getRepeatCount() == 0) {
                        state.startTracking(event, this);
                        Log.i(TAG, "KeyEvent.ACTION_DOWN");
                        return true;
                    } else if (event.getAction() == KeyEvent.ACTION_UP
                            && !event.isCanceled() && state.isTracking(event)) {
                        if ( mCallback.editTextOnBackPressed() ) {
                            Log.i(TAG, "KeyEvent.ACTION_UP | cancelando processo padrão");
                            return true;
                        } else {
                            Log.i(TAG, "KeyEvent.ACTION_UP | processando o processo padrão");
                            return super.dispatchKeyEventPreIme(event);
                        }
                    }
                }
            }
        }
        Log.i(TAG, "Returning generic onBackPressed");
        return super.dispatchKeyEventPreIme(event);
    }
    
    
    public interface BackPressed {
        /**
         * Listener para eventos onBackPressed com o teclado presente.
         * O objeto inscrito tem a oportunidade de processar o evento
         * e definir se o <code>View</code> deve seguir ou não com sua
         * ação padrao.
         * @return  true: <code>View</code> abandona a ação padrão e executa
         *                  somente o bloco definido antes do retorno
         *          false: <code>View</code> executa o código definido antes do
         *                  retorno e dá sequência a ação convencional
         */
        boolean editTextOnBackPressed();
    }
    
        
    25.02.2016 / 01:43