Today I noticed something rather interesting. The activity lifecycle seems to have a slight discrepancy when the phone display times out. Let me explain with an example.
I have an activity that uses the accelerometer to vibrate the phone. In the onPause
event I unregister the SensorManager
listener so that I don't vibrate the phone when my activity is no longer the main focus.
However, I've noticed if the display shuts off and then comes back on my activity has the SensorManager
listener registered even before I unlock the screen, enter my password, and my activity is shown.
Now I realize this is my own interpretation of how I would expect it to work, but to me this seems rather strange, since my activity isn't yet the main focus. I expected the SensorManager
listener wasn't registered because onResume
hasn't yet been called. This clearly isn't the case when I can make my phone vibrate from both the lock screen and the password screen.
So, can anyone explain why this behavior? Secondly what can I do to get around it?
Thank you.
EDIT for clarity
I use the accelerometer to trigger a vibrate by moving the phone. This is accomplished through the SensorManager
listener.
Scenario:
My activity is in the foreground (main focus). I trigger the vibrate by moving the phone. The display times out. At this point I can not trigger the vibrate. I press home/power to show the screen. I can now vibrate my phone even though either the lock screen or password screen is shown and my activity is not in the foreground.
I can not verify if the reason I can not vibrate the phone when the display turns off is because onPause
was called or there is something inherent to the OS that prevents it. Or to put it another way, I also can't verify if onResume
was called when the display turned on.
The key to all this is understanding the activity life cycle when the phones display is shut off. Unfortunately, my expectation was that it would follow the same life cycle diagram that we have all learned. My opinion was that it is different.