Search code examples
androidonbackpressed

Android how to make back button perform action on a the first click?


I have back button reprogrammed like that:

@Override
public void onBackPressed() {
    returnManager();
}

The problem that I am facing is that when I touch it for the first time it doesn't do anything. After the first time, it works as intended and calls returnManager. Then, if I press some other buttons on the screen and press back again, it doesn't do anything for the first time but works on next touches. Is there anything I am missing here?


Solution

  • A strange solution:

    int calls = 0;
    
    @Override
    public void onBackPressed()
    {
        calls++;
        if(calls==1)
        {
            returnManager();
            returnManager();
        }
        else
        {
            returnManager();
        }
    }