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 screen
I 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.
In your finish() method, you want to use isActivityVisible() to check if the activity is visible or not. There you can also check if the user has selected an option or not. Continue when both conditions are met.
Using activity. isFinishing() is the right one solution. it return true if activity is finished so before creating dialog check for the condition. if true then create and show dialog.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With