I've been using the static method newInstance to create fragments after reading numerous recommendations to do so here on SO (most notably, here). Before that, I had been putting variables in the constructor (bad) and checking if the fragment exists using 'findFragmentByTag' and reusing that fragment if it exists.
My question... Should I still be using 'findFragmentByTag' if I'm getting my fragment using myFragment.newInstance() or should I just create the new instance and let the garbage collector take care of the previous one?
if I should be using findFragmentByTag, what's the best method of ensuring the reused fragment takes updated parameters.
Example:
String test;
test = "hello";
launchNewFragment();
// User hits back, does something else, then goes back to that fragment
// in this call, how can I ensure the reused fragment is getting the new value of 'test'?
test = "world";
launchNewFragment();
private void launchNewFragment() {
Fragment f = getSupportFragmentManager.findFragmentByTag("myFragment");
if (f == null) {
f = myFragment.newInstance(test);
}
getSupportFragmentManager().beginTransaction().replace(R.id.myContainer, f, "myFragment").addToBackStack(null).commit();
}
Thanks all