1
I have an Activity with a view pager, and this one controls three fragments, F1, F2, F3. The F1 fragment shows user-added items, the F2 fragment loads all items from an external server, showing them in a list, and finally, the F3 fragment shows the items the user has decided to store. User saved items are saved in txt, so they do not need server access.
The problem: Whenever the user saves an item, I need to add a mark on the item selected in F2, and automatically add it in F3, and the same happens when the user unchecks in F3 and the item needs to be updated in F2, but in this case only the layout, as it has nothing to do with external connections.
The solution created by me: Leave the public and static adapters in Activity and create methods within it that update the adapters whenever there is an action, for this, I had to create a class that extends the class Application
, to create a global context.
Doubt: It is known that it is not good to create static contexts due to memory leakage problems, but I believe that this should happen because keeping code working during other activities, really must weigh. But using this solution between fragments, since these are always working in the foreground, can also bring risks of memory leakage? Noting that I only use the static context in this Activity so that the adapters can be updated by other fragments.
Thanks in advance!
Is it not possible to create a Singleton to support the adapters/data? Thus, it is not necessary to mess with context and the application. It has a lib called eventbus, which serves for this type of problem. It sends a message to "nothingness" and a certain method will be "listening" if that message was called. http://greenrobot.org/eventbus/documentation/how-to-get-started/
– Mr_Anderson
This idea sounds interesting. But I could implement it even if my adapters need context-like parameters?
– Guilherme Ramos
I didn’t understand the problem. can specify more?
– Mr_Anderson
The adapters I’m using need the Context parameter, and my question is, if using Eventbus, can the context be used in this library? He wouldn’t end up being static either?
– Guilherme Ramos
I don’t think so. Normal variables within static methods continue to be normal variables. So, I guess nothing will change.
– Mr_Anderson