2 years ago

#46311

test-img

Neco

Is it possible to register sensors and listen values in Fragment?

I have a library where i can listen and gather sensor values. I can do it in Activity without a problem. But when in Fragment, i couldn't find a proper solution. My Fragment implements SensorEventListener and overrides onSensorChanged() but it returns nothing. My code is as below;

class HomeFragment : Fragment(), SensorEventListener {

    
    ...

    override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {}

    override fun onResume() {
        super.onResume()

        myLibrary.registerSensors(this.requireContext())
    }

    override fun onPause() {
        myLibrary.unregisterSensors(this.requireContext())
        super.onPause()
    }

    override fun onSensorChanged(event: SensorEvent?) {
        myLibrary.onSensorChanged(event)
    }
}

kotlin

fragment

sensors

0 Answers

Your Answer

Accepted video resources