Track parent class from element image

I have class A with an ImageView instance variable. I am currently using setTag () to go from ImageView back to an instance of class A. Can this circular loop? I also heard about the mention of weak links. What is the best way to get the parent class of A ImageView?

My use case is dragging and dropping an ImageView around the screen, and I need to access class A to get the drag and drop information for ImageView.

+6
source share
3 answers

I believe that Dalvik VM has no problem detecting circular references.

Here's a Q / A that discusses this: Circular References in Java

Only a very naive implementation will have problems with circular links.

I would use setTag to bind a link to a related object. In addition, using setTag / getTag on View is a standard and recommended approach for achieving various optimizations on Android, even those found in Android samples. Some links:

http://www.vogella.de/articles/AndroidListView/article.html#ownadapter_viewHolder

Android User List

If the Android GC has a problem detecting such dependencies, Android will not work the way it works. Go ahead and use View.setTag if that suits your purpose.

+3
source

Circular references in java are not a problem for GC. This is not a problem because of a concept called “reachability”.

You can consider links to objects as a directed graph, where nodes are objects and edges are links to objects. The graph has a set of nodes called "root nodes" - these are (approximately) objects that are directly accessible to some stream in vm. Things like local variables when calling any function on the stack, static fields in classes, etc.

When performing garbage collection, the virtual machine starts up with these root nodes and marks them as “used”. Then, for each edge leading from these used nodes (i.e., for each object referenced from the used objects), it is also marked as used, and so on, until there is nothing left. And then, any objects that were not marked as “used”, he knows that he can safely collect garbage.

Now, say, for example, you are using some kind of method, and you have the following code:

a = new A(); ab = b; b = new B(); ba = a 

You now have a circular link between a and b. And both of these objects are part of the set of roots, because they are a local variable in a method call that is on the stack.

Then, when you exit this method, both a and b will no longer be part of the root set. And until you link to them somewhere else, there is nothing that could contain a link to a or b.

So, now your graph of links to objects will have a small disabled part of the graph containing a and b. Both of them still have references to each other, but nothing else refers to them. Since they are not accessible from the root set, GC knows that they are not used and can garbage collect them even if they have links to each other.

(note that this is a slightly simplified description of garbage collection, but it is still a useful / reasonable mental model of how this works)

So, the short answer is: “Yes, you have a circular link, but that’s not a problem, because both objects will be assembled when both already do not reach”

+1
source

The best way to get rid of circular references is to make the instance variable in the final variable:

  private final ImageView myImageView; 

This will force you to initialize the variable in constructor A. If you explicitly pass the link to ImageView in new calls, you will avoid the chances of circular links.

  public A(ImageView iv, ...) { this.myImageView = iv; ... // associate this instance of A with ImageView? this.imageView.setTag(this); ... } 
0
source

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


All Articles