문제

I am making an application that attaches both location and sensor listeners, so that it can then record changes from them into a file.

What's strange is that one of the phones using the application was Force Closing in some instances, which I narrowed down to orientation changes: if the phone changed orientation during recording, the SensorManager attribute I had stored inside the activity as 'mSensorManager' would then be set to null.

This leads into my question: should I not store the SensorManager reference, due to it being a bad design choice? Or is it okay, and should I just check if the reference is null before using it?

EDIT: Two things to add:

  1. I found out that not only the accelerometer and magnetometer attributes were also nullified (which kind of made sense), but also the locationmanager (which doesn't make much change, as it has nothing to do with orientation determination);

  2. Should I store those attributes inside something other than the activity, that won't nullify the references upon an orientation change?

What did it for me: Correcting listener, manager and sensor instantiation to be done from within the activity's lifecycle. Then the only problem was with the wake-lock, which was destroyed, but I needed to be kept through the orientation changes. I changed it to be acquired during the activity lifecycle aswell, albeit in a different phase.

도움이 되었습니까?

해결책 2

What did it for me: Correcting listener, manager and sensor instantiation to be done from within the activity's lifecycle. Then the only problem was with the wake-lock, which was destroyed, but I needed to be kept through the orientation changes. I changed it to be acquired during the activity lifecycle aswell, albeit in a different phase.

다른 팁

  1. try after adding this line inside activity tag in you manifest

     android:configChanges="orientation|keyboardHidden"
    

    I guess it is nullifying because on orientation change onCreate is called for that activity

  2. Try to save values using shared preferences
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top