Action on the back button when the keyboard is active

Asked

Viewed 390 times

2

I am trying to use the back button action to show a menu in my application, with the onKeyUp method. Once 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 normal menu, in the third is a search screen, and when I go to it, the keyboard automatically goes up, and the menu disappears, because at this moment it is unnecessary.

My difficulty is showing this menu again after being clicked the back button, it works in a certain way, but only appears in the second click of the back button. That is, the first click only serves to download the keyboard, and the second that appears the menu. But I want that in the first click already download the keyboard and appear the menu, and I am not getting. Follow the methods, all this 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 doesn’t work at first click ?? How would it work ?? I have two options: identify when the keyboard is downloaded, or when the back key is clicked, to show the menu. I accept suggestions since jpa thank you.

1 answer

0

This is possible, but unfortunately the Android SDK does not offer a simple way to resolve this issue. Before you try to implement the solution I would strongly recommend reflecting on whether you really need to intercept the onBackPressed() when the keyboard is present.

My answer is based in this enlightening contribution

  1. create a custom version of View where you need to recover onKeyPressed
  2. Subscribe dispatchKeyEventPreIme(KeyEvent event)
  3. Process the event your way. In the snipped below I chose to process the event in the entered object and return a result for the custom view to proceed or not with your default actions.
  4. Create an interface to receive the event onBackPressed in View
  5. Register your Activity (or other object) to receive the event

OBS 1 - You’ll need to understand the bare minimum of custom View creation.

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

OBS 3 - You can check on my github the full 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();
}
  • Because it’s a friend, but I don’t understand very well about custom view. You could explain in more detail ?

  • Comrade, these are many steps to explain about Custom View and the answer would be even outside the topic of your question. The answer to your original question has already been given. Your difficulty in implementing a Custom View is already another question. I should post on my www.tinmegali.blog with a tip on this soon. You can wait or open a question about Custom View at stackoverflow. Good luck.

  • I got it, the way I said it worked. Obgggg

Browser other questions tagged

You are not signed in. Login or sign up in order to post.