Android: Using weak link to instance of activity / fragment instead of strong link?

In my Android application, I have many actions and fragments, and for the code to be read and reused, I have helper classes for each of the actions and fragments. These helper classes are responsible for everything from filling dynamic representations to checks and network-related tasks.

I am currently passing an Activity / Fragment instance for each of the helper classes. I understand that each of the assistants is created as part of the action and will be destroyed along with the activity.

Are there any advantages / disadvantages of passing a weak link to my helpers instead of a strong link?

+3
source share
1 answer

As long as the helper's lifetime is in the lifetime of the Activity, then there is no need to use WeakReference. If the helper can live longer than Activity, then you should use WeakReference to avoid maintaining activity in the objects graph when the system destroys it.

+5
source

Source: https://habr.com/ru/post/1270499/


All Articles