Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onResume being called over and over while phone screen is locked

Tags:

android

It appears that if an activity is in the foreground while the phone's screen is locked, broadcast events and/or wakelocks will cause that activity's onResume to be called, whether they were intended for your app or not. This happens even if you don't interact with the phone in any way. Don't unlock it, don't touch it, don't do anything, and your onResume will be called over and over without a corresponding onPause.

We noticed this because we log a tracking event onResume, and we noticed that some devices were getting a TON of these onResume events. We were able to repro and noticed that it happened every time any app seemed to receive an intent (such as SugarSync, Google Voice, or the passive location listener in our own app).

We noticed this on the Galaxy Nexus and Galaxy S3 running ICS. Interesting to note, we couldn't reproduce the issue on Jelly Bean.

Anyone have any pointers to what might be happening here?

like image 844
emmby Avatar asked Jul 30 '12 23:07

emmby


1 Answers

I observed the same behavior on several phones running Gingerbread and ICS. Your description matches what I have seen. For some reason, the activity has the onResume triggered, even though the screen is still locked. Perhaps it resumes do to some operating system level event?

The best direction from the android docs seems to be from this paragraph in the onResume definition:

Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user (for example, to resume a game).

When developing your Activity, keep in mind that onResume may be called multiple times in quick succession, without any direct user interaction. So basically, don't put any code in onResume that can only be run once, or is designed to trigger an immediate response from a user. You have to decide for yourself whether onResume or onWindowFocusChanged works better for what you want to do.

like image 175
jacobhyphenated Avatar answered Oct 21 '22 04:10

jacobhyphenated