Lets say I have 2 fragments, one of which has a text input field.
I switch between my fragments in the main activity with this:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_new) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, NewFragment(), fragment.getClass().getSimpleName())
.addToBackStack(Integer.toString(id))
.commit();
} ...
Notice I'm using framentTransactions replace
method
On the current fragment with the input box, I type in some text. Then I use the nav bar to switch to a new fragment. I would expect that because I'm using replace
, the old fragment no longer exists.
I navigate to the new fragment fine, but when I press the back button, the old fragment still has the text that I typed in previously
Why would this happen when using replace
? Am I misunderstanding how fragment transactions work?
Does the onCreateView
method of the fragment not get called again when I press back, thus essentially resetting the fragment?