Search code examples
androidaccessibilityservice

when to use event.getSource() and getRootInActiveWindow() in Android AccessibilityService


I am trying to get to grips with Android AccessibilityService by reading the official docs and various posts here on SO and applying code samples.

Even after that, I am still unsure when I should use event.getSource() and getRootInActiveWindow() in the onAccessibilityEvent(AccessibilityEvent event).

Anyone who can explain the difference and the usecase for each of them?


Solution

  • event.getSource()

    Used when you want to know what View the event was triggered for.

    EX: Such and such a button was pressed or such and such layout was scrolled.

    somAccessibilityServiceInstance.getRootInActiveWindow()

    Used when you want to respond to an event by manipulating or viewing the content on the whole screen. Example, a screen changed event comes in, and you want to update some cache you have of on screen elements. Or perhaps to check on the state of some component unrelated to the event. The possibilities for getRootInActiveWindow are endless.