简体   繁体   中英

Overriding behavior of hardware back button on an actionbar

I'm using an action bar than when its action overflow is pressed shows the options of a menu I've defined, when pressing the hardware back button that options close as expected.

But I want to offer the user the possibility to trigger another behaviour if pressed, so I have to override how it reacts to the pressing of the hardware back button when items are being displayed.

So far I've tried the following:

       myToolbar.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {

            if (i == KeyEvent.KEYCODE_BACK) {
                finish();
                return false;
            }
            else
            {
                return true;
            }
        }
    });

As expected this code doesn't work, it doesn't even fire when the hardware back button is pressed.

I've also tried to create my own toolbar by extending from Toolbar with the following class to be able to override its onKeyPreIme event:

public class LockToolbar extends Toolbar
{
private KeyImeChange keyImeChangeListener;

/* Must use this constructor in order for the layout files to instantiate the class properly */
public LockToolbar(Context context)
{
    super(context);
    // TODO Auto-generated constructor stub
}

public void setKeyImeChangeListener(KeyImeChange listener){
    keyImeChangeListener = listener;
}

public interface KeyImeChange {
    public void onKeyIme(int keyCode, KeyEvent event);
}

@Override
public boolean onKeyPreIme (int keyCode, KeyEvent event)
{
    if(keyImeChangeListener != null){
        keyImeChangeListener.onKeyIme(keyCode, event);
    }
    return false;
}

}

The class on its own at the very least would compile correctly, but problem is than the instruction setSupportActionBar defined for class toolbar needs a Toolbar object to work properly, it cannot support a lockToolbar.

I know I could override the method setSupportActionBar but this looks very very long to do, I guess there has to be some easier solution.

Any idea on how to approach this issue?

I finally got to solve it with a code like this:

 @Override
    public void onPanelClosed(int featureId, Menu menu)
    {

       timesclosed++;

        if (exitinmenu)
        {
            finish();
        }
        else
        {
            if ((exitinmenu2) && (timesclosed==1))
            {
                    finish();
            }
            else
            {
                if ((exitinmenu3) && (timesclosed==5))
                {
                    finish();
                }
            }
        }

        if (timesclosed==8)
        {
            timesclosed=0;
        }
    }

Whenever I press the hardware back button if the menu from the action bar is displaying, the event is fired, in fact it fires 4 times, with timesclosed and exitinmenu I can control where the menu has closed. I wonder why it fires 4 times but well, it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM