How to find the "last" back press?

I have Activity

with Fragment

, and have a lot of transactions, so the back button most often discards Fragment transaction

. But I need to detect the last backpress.

I can't use the method onPause

because I don't want to do anything when the user presses home or starts another activity.

+3


source to share


2 answers


Use this getFragmentCount method below.

It will return a counter Fragment

if it is 0

, and then you press one last time



private int getFragmentCount() {
    return getSupportFragmentManager().getBackStackEntryCount();
}

      

call this method in onBackPressed()

yourActivity

+9


source


Don't know if this helps, but here is the code to catch if the button was clicked:



@Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if(keyCode ==KeyEvent.KEYCODE_BACK)
        {
            //Do stuff here
        }
        return super.onKeyDown(keyCode, event);
    }

      

0


source







All Articles