I was studying Fragments and got little confused on differentiating FragmentTransaction.replace(id, fragment, tag)
and FragmentTransaction.addToBackStack(tag)
calls. Lets say that my current fragment is FragmentA and then I loaded FragmentB. I want that in future, when I need to load FragmentA, I don't have to reload it. Just load the old one in old state. I used the following code segment:
public void loadFragment(Fragment fragmentB, String tag) {
FragmentManager fm = getSupportFragmentManager();
View fragmentContainer = findViewById(R.id.fragment_container);
FragmentTransaction ft = fm.beginTransaction();
ft.replace(fragmentContainer.getId(), fragmentB, tag);
ft.addToBackStack(tag);
ft.commit();
}
Now I am confused, where should I add the string tag? In replace()
or in addToBackStack()
or in both calls? Can you explain the difference between these two tag places?