Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: passing WeakReference as a parameter

Tags:

java

android

I'm using a WeakReference for ImageView in my (Obj_A), and I pass this ImageView WeakReference to another object (Obj_B) that assign it to an ImageView variable (But not a WeakReference as well), so will the receiver ImageView variable have the nature of the WeakReference?

I hope I managed to mold my question correctly, and I hope you understand what I mean.

like image 559
Ashraf Alshahawy Avatar asked Sep 16 '26 23:09

Ashraf Alshahawy


2 Answers

No, your ImageView in Obj_B will not be a WeakReference. A WeakReference allows you to store a reference to an object in such a way that if that object has no other "strong" references anywhere, it will be garbage collected. By removing the WeakReference wrapper in Obj_B, you're now taking the object that your WeakReference was managing and storing your own strong reference to it.

like image 133
Joseph Roque Avatar answered Sep 19 '26 14:09

Joseph Roque


A weak reference is a reference that does not protect the referenced object from collection by a garbage collector. Without seeing your code it is not possible for me to say if what you have done has any issues.

I suggest that you have a look at the following post:

How to use WeakReference in Java and Android development?

like image 27
PaulT Avatar answered Sep 19 '26 14:09

PaulT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!