This snippet of code:
@Override
public void onBackPressed() {
if( !getFragmentManager().popBackStackImmediate() ) super.onBackPressed();
}
What does this mean exactly? I looked in the docs and it says
"Like popBackStack(int, int), but performs the operation immediately inside of the call. This is like calling executePendingTransactions() afterwards."
But I don't know what this means, or what it means to have the negation in front of it, or what super.onBackPressed()
is doing.
Returns true if there was something popped, else false.
Basically you are popping your backstack (cleaning the fragments on the history, so when you go back you will not go to the last fragment) and if theres no fragment on the backstack (no fragment on the history) you will do your super.backPressed. The super.onBackpressed() will execute your activity onBackPressed (even if this code is called from a fragment, as there is no default code to do on your fragment onBackPressed(). If your activity extends a default Android activity it will go back to the last activity, if theres one, os get out of the app, if theres none.
pseudocode:
popBackStack() // clear the fragment history
if (somethingWasPopped) {
super.backPressed() // this will do what i said above
}
Probably the developer who done this was trying to make the user unable to get back to the last fragment when he press back. Like Activity1 -> Activity 2 (frag A -> frag B -> frag C). Now, user is on frag C, he will get back to Activity 1 (when he press back or click on back icon), instead of frag B.