How to check if activity is visible? onResume is not enough

One of my actions is VideoViewto play some content. I implemented the suspension / resumption of the video in onPause()and onResume(), accordingly, but to my surprise - onResumeit is called before the action is really visible to the user.

To be precise, the scenario is as follows:

  • screen activity, video playback
  • the user locks the phone with the power button.
  • activity receives onPause(), video stops
  • user presses power button
  • activity gets onResume()(and thus resumes the video) before the user unlocks the screen

I have confirmed this behavior of Android 2.2, 2.3 and 4.0. I assume that this is intentionally done so as to allow the activity to prepare for redrawing immediately after the screen locks.

How can I determine when the action really appears to the user? I tried to wait for it to onWindowFocusChanged(true)be called, and it seems to work, but that does not make me completely safe.

+5
source share
2 answers

You need to wait for the onWindowFocusChanged event, as well as the OnResume event. This is a well-known issue that is no longer encountered in Android 4.1+. Details can be found at http://android-developers.blogspot.co.uk/2011/11/making-android-games-that-play-nice.html

+8
source

onResume , , onResume(), .

BroadcastReceiver, , : Intent.ACTION_USER_PRESENT, onReceive .

Intent.ACTION_USER_PRESENT:
, , (, ).
, .

0

All Articles