Skip to content Skip to sidebar Skip to footer

Refreshing For Constant Fragments

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

Solution 1:

Finally I understood the fragment transaction behavior and how it works ...

Once you add/replace new fragment to Fragment Transaction you object is saved as initial state within Fragment Manager and when you try to detach the fragment and reattach [the same object] the Fragment Manager will get the object state which you passed it before to add()/replace() function. So that's the reason of the result I got.

To simplify it.

  • Suppose that you have an Class A extends from Fragment class and this class contains Integer object have no value and have Set()/Get() methods.

  • Now create an instance from this Class A and call Set() function and pass value 1 to it. Then pass this instance to Manager Transaction by add()/replace()` method.

  • Now try to call Set() function and change the Integer object to another value.

  • Detach the class A and reattach it and check the value of this object, you will get that the value of object return to initial value you assigned before passing the object to Manger Transaction

Beside that there's an important note ... Fragment Transaction Actions like add(), replace(), remove() ... don't work at main thread and that's the reason of using commit() method and commitNow() method .. because when you use commit() your action will add to Fragment Transaction Queue so it will be executed after a bit of time but not now that's upon its order inside the queue, and so that you have another method called commitNow() to make your action execute immediately.

Post a Comment for "Refreshing For Constant Fragments"