Pitel Pitel - 3 months ago 13
Android Question

How to detect "last" back press?

I have an

Activity
with
Fragment
, and there is a lot of transactions, so the back button most often just roll back
Fragment transaction
. But I need to detect the last back press.

I can't use
onPause
method, because I don't want to do anything when the user presses home, or starts another activity.

N J N J
Answer

Use this getFragmentCount method below.

It will return the Fragment count if it is 0 then your are in last back press

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

call this method in your onBackPressed() of your Activity