Strange lifecycle behavior in my Screen Saver App
I just developed a screen saver app and I found a strange behavior in its lifecycle. My work flow is like this:
- start my
RegisterService
, where I callregisterReceiver
method to register aBroadcastReceiver
, which can receiveACTION_SCREEN_OFF
.
2.In the onReceive
method of this BroadcastReceiver
, I start an activity as the screensaver.
3.In the activity, I wr开发者_开发技巧ite Log.i()
statement to track its running.
My question is:
When the screen times out, or when I press the POWER key, the screen turns off, and the system will send ACTION_SCREEN_OFF message. As I expect, my receiver starts the screen saver activity. However, I find this Activity calls onCreate()
, onResume()
, onPause()
, onResume()
sequentially according to the output in logcat.
It seems as if some a activity comes at front of my screensaver and finishes immediately, so my screensaver calls onPause()
and then onResume()
.
Any idea? This problem handicaps me in programming, please help. Thanks!
Well based on a brief study of the PowerManagerService.java source code, when it's time to turn the screen off, the system initiates an animation (look at line 2183 for the class source) to do that. That means that your activity will pause and then will resume after the animation has ended.
I cannot be 100% sure for this, since I haven't tested it in my environment but this is the only logical explanation I found for your situation.
Hope this helps...
I can recommend you something very easy that might work for you, if you do not want the pause behavior why don't you try to Override the method onPause()
and just do nothing :P don't call super.onPause()
and that will terminate the behavior of it.
Other thing that might work for you, declare a static variable, add 1 on the "onResume()" method and return to "0" when "onStop()" is called. now just evaluate when the "onResume()" is called and if the variable is "0" then is the first time, anything else do nothing.
I hope this helps cause there is no much information on your question to be more specific.
精彩评论