Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AccessibilityNodeInfo refresh() and recycle()

Tags:

android

I have read through the android documentation in https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html

I don't understand the description stated in the document about recycle() and refresh() method.

1. recycle() - Return an instance back to be reused.

  • The instance is return back to where?
  • In which scenario this instance going to be reused?
  • AccessibilityNodeInfo might contains child node, do i need to call recycle() when my code navigate to each of the node or just call recycle method at root node?

2. refresh() - Refreshes this info with the latest state of the view it represents

  • I thought when onAccessibilityEvent() method was called, the AccessibilityEvent object should contain the latest state?
  • AccessibilityNodeInfo might contains child node, do i need to call refresh() when my code navigate to each of the node or just call refresh method at root node?
like image 289
kenn3th Avatar asked Jul 05 '17 09:07

kenn3th


1 Answers

The Android Accessibility API uses a pool of AccessibilityNodeInfo nodes. That way, iterating over large trees will not create many objects that would slow down the garbage collector. In other words, when you recycle() a node, you might later (on the next event for example, or when iterating over the same tree) receive the exact same node object again, but filled with completely different details. That's why it is important you don't hold references to nodes you have recycled (and for example try to compare them to other nodes).

When you obtain child nodes, you have to recycle each child node. When you do not get them, you do not need to recycle them. You can recycle children before you recycle parents, or the other way round, depending on how long you will need access to the objects.

When you receive a node, it contains the latest state. But when you perform an action on it (e.g. click or scroll), the state of the node or of other nodes may change. If you want to see these changes in real-time (and not only when you receive the next event), you have to refresh() the node (or you can refresh() the root and try to obtain a new copy of the node from the root)

When you freshly obtain child nodes, you do not need to refresh them (they are already fresh). You only need to refresh nodes that you obtained earlier (before doing some interaction with them or with other nodes).

like image 188
mihi Avatar answered Oct 06 '22 01:10

mihi