I will try to describe the problem and simplify it At my app, I have one activity, and 3 fragments and I swap through them at Phone Mode. and these fragments I made their fragments instances as constant objects at main activity so the internal objects which are not related to fragment life cycle stay hold their values even this fragment doesn't visible (Logically the objects will release when the fragment object is destroyed)
Now, when I tried to make my app support tablet, I made the 3 fragments display at the same time but there's something strange is happened.
Say fragment A hold list of items and fragment B show the detail of selected item. and there is an object at fragment B. this object holds the selected item id [All fragments display at the same time in one activity as I mentioned before], this object save current item id value when the fragment B created, and save the new selected item id when onDestroyView()
method called at fragment B.
Now, when I start my app, I add the fragment A, and when I select item from list the fragment B is added and its object hold the selected value. for now all things are right. when I select another item, I just need to refresh the fragment B as the internal object will hold the new selected id so I did this line
Fragment fragment = mFragmentManager.findFragmentByTag([fragmentId]);
mFragmentManager.beginTransaction().detach(fragment).attach(fragment).commit();
but the result I got is that the fragment B get the previous state which it was on, what I mean is that the internal object hold the first item id i selected.
I tried to test the internal object before fragment refreshing
Fragment fragment = mFragmentManager.findFragmentByTag([fragmentId]);
Log.e("my object","value" + String.valueOf(fragment.getValue));
mFragmentManager.beginTransaction().detach(fragment).attach(fragment).commit();
and It was holding the right item id value which have to display ... !! Also I passed the constant value instead to ensure but I get the same strange result
Fragment fragment = mFragmentManager.findFragmentByTag([fragmentId]);
mFragmentManager.beginTransaction().detach(fragment).attach(FRAGMENT).commit();
So what's happen ?? Logically it have to work as I think.
pic for the original code and this is the log result (Note: 2 is the next item i selected) and this is the next log result after fragment B is refreshed (Note this log inside fragment B and id 3 is the first Item i selected) the expected result is 2