Search code examples
androidandroid-fragmentsandroid-sensors

Multiple fragments with same sensor


I am writing a 'dash-board' application which includes several instruments based on the barometric sensor (for phones where this is available) and instruments using the position. The instruments are implemented as fragments.

The question is if it is best practice to manage the sensor-interface in the host activity, register the host activity as sensor event listener, and call the fragments methods with the sensor event, or to interface the SystemService and register listeners etc. through each fragment individually?

My main concern is battery consumption and possible 'collisions' with fragments blocking each other while accessing the same resource.


Solution

  • The question is if it is best practice to manage the sensor-interface in the host activity, register the host activity as sensor event listener, and call the fragments methods with the sensor event, or to interface the SystemService and register listeners etc. through each fragment individually?

    For things that transcend the bounds of a single fragment, having the activity handle it is certainly reasonable.

    My main concern is battery consumption

    This shouldn't be a problem -- it's not like multiple sensors will be powered on because there are two with registered interest in the barometer. Heck, for all you know, there are other apps also using the barometer.

    and possible 'collisions' with fragments blocking each other while accessing the same resource

    That too won't be a problem, any more than any other main application thread contention.