Reputation: 7275
One of my activity embeds VideoView
to play some content. I implemented pausing/resuming the video in onPause()
and onResume()
, respectively, but to my surprise - onResume
is called before the activity is really visible to the user.
To be exact, the scenario is as follows:
onPause()
called, video stopsonResume()
called (and thus resumes video) before the user unlocks the screenI confirmed this behaviot with Android 2.2, 2.3 and 4.0. I guess it is intentionally done like that, to let the activity prepare to redraw itself immediately after locking screen goes away.
How can I detect the moment when the activity actually appears to the user? I've tried to wait for onWindowFocusChanged(true)
being called, and it seems to work, but it doesn't make me feel perfectly safe.
Upvotes: 5
Views: 9744
Reputation: 1651
You need to wait for the onWindowFocusChanged event as well as the OnResume event. It's a well known issue that no longer occurs in Android 4.1+. Details are available at http://android-developers.blogspot.co.uk/2011/11/making-android-games-that-play-nice.html
Upvotes: 8
Reputation: 16043
In general onResume
should be enough, but for your special case I believe you should resume the video not on onResume()
, but when the user has unlocked his screen.
To achieve this you will need to make a BroadcastReceiver
that listens for an intent called: Intent.ACTION_USER_PRESENT
, and in onReceive
resume your video.
Intent.ACTION_USER_PRESENT:
Sent when the user is present after device wakes up (e.g when the keyguard is gone).
This is a protected intent that can only be sent by the system.
Upvotes: 0