
Making Android Games that Play Nice - DanielRibeiro
http://android-developers.blogspot.com/2011/11/making-android-games-that-play-nice.html
======
seanalltogether
_Fixing this is trickier than it sounds. Some games wait for
onWindowFocusChanged() instead of onResume(), which works pretty well on
Gingerbread. But on Honeycomb and higher, onWindowFocusChanged() is sent when
certain foreground windows — like, ironically, the volume control display
window — take focus. The result is that when the user changes the volume, all
of the sound is muted. Not the developer’s original intent!_

 _Waiting for onResume() and onFocusChanged() seems like a possible fix, and
it works pretty well in a large number of cases. But even this approach has
its Achilles’ heel. If the device falls asleep on its own, or if the user
locks the phone and then immediately unlocks it, your app may not receive any
focus changed messages at all._

This doesn't give me a lot of confidence in the framework decisions being made
by the android team.

~~~
simoncion
The solutions presented seem simple enough. Here's the "I must resume my
activities immediately!" solution:

 _1\. Pause playback when you receive onPause().

2\. When you receive onResume():

2a. If you have previously received an onFocusChanged(false) message, wait for
an onFocusChanged(true) message to arrive before resuming playback.

2b. If you have not previously received an onFocusChanged(false) message, then
resume audio immediately.

3\. Test thoroughly!_

The documentation for onFocusChanged might be helpful for you:
[http://developer.android.com/reference/android/view/View.OnF...](http://developer.android.com/reference/android/view/View.OnFocusChangeListener.html)

------
pyoung
Alternatively, don't include background music in your game. I am usually
listening to my own music anyway.

------
aw3c2
Wow, new low for Javascript non-fallbacks. That page has no scrollbar in my
default setting (Opera with Javascript disabled) so the text is cut off mid
way through the post.

~~~
nostyle
Agreed, almost as bad as the blank page used by some sites (like Google+
posts). In this case the workaround was the same.

In Firefox I was able to get scrolling by disabling the page style (View->Page
Style->No Style). I'd imagine there's something similar in Opera.

------
TheRevoltingX
Too short! I was hoping for more tricks and tips. I started learning Android
programming by making a game. (<http://developingthedream.blogspot.com/>) It's
tricky to get things like multiple activities, dialog activities, efficient
service communication. Then of course, multiple displays and sizes. Also,
different keyboard layouts, etc.

~~~
DanielRibeiro
Sounds interesting! Why don't you should write about it?

~~~
TheRevoltingX
Hmm, I have been meaning to make a tutorial on something Android related. They
tend to bring in traffic to my blog :p

