Interface HealthBackgroundListenerFactory


public interface HealthBackgroundListenerFactory

Creates HealthBackgroundListener instances after the app's process has been killed and relaunched.

Why this exists rather than reflection

When the operating system relaunches an app in the background to deliver health data, the only durable record of which listener to invoke is whatever was persisted -- a name, not a Class. Resolving that name reflectively is the obvious approach and the wrong one on this framework's targets:

  • A class referenced only by a string is invisible to the iOS and JavaScript translators' dead-code elimination, so it can be stripped out of the very build that needs it.
  • Obfuscation renames the class, so a name persisted by an earlier version of the app no longer resolves.

So the binding is produced at build time instead. The build server scans for implementations of HealthBackgroundListener, generates a factory that constructs each one with a direct new expression -- a real reference, which dead-code elimination and obfuscation both follow correctly -- and registers it through HealthStore.setBackgroundListenerFactory(HealthBackgroundListenerFactory) during app startup.

Application code never implements or calls this.

  • Method Summary

    Modifier and Type
    Method
    Description
    create(String className)
    Creates the listener registered under className, or returns null when this build has no such listener.
  • Method Details

    • create

      HealthBackgroundListener create(String className)

      Creates the listener registered under className, or returns null when this build has no such listener.

      className is the source-level class name recorded when the subscription was registered. It stays valid across obfuscation because the build server emits a keep rule for every class it generates a binding for.