Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the Activity is visible? onResume is not enough

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:

  • activity is on screen, video is playing
  • user locks the phone with power button
  • activity gets onPause() called, video stops
  • user presses power button
  • activity gets onResume() 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.

like image 459
Code Painters Avatar asked Aug 22 '12 10:08

Code Painters


People also ask

How do you know if activity is visible?

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.

How do you check if activity is finished or not?

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.


1 Answers

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

like image 96
GeekYouUp Avatar answered Nov 01 '22 08:11

GeekYouUp