[Solved] Fragments addToBackStack Closing the application


Suppose you have 3 fragments

(1) Fragment A

(2) Fragment B

(3) Fragment C

You are going A to B then B to C then Backpress will return to B then again Backpress will return to A then Backpress will close app.

Method for replace Fragment by adding Tag:

Pass your fragment and tag as arguments in below method.

private void replaceFragment(Fragment frag, String tag) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.realtabcontent, frag);
    transaction.addToBackStack(tag);
    transaction.commitAllowingStateLoss();
}

No need to write onBackPress() or fragmentManager.popBackStack(), it will automatically handle.

Done

12

solved Fragments addToBackStack Closing the application