views:

1439

answers:

1

Hello all,

I have an app that starts playing sounds and begins/resumes gameplay in the onResume() method, but what I'm noticing is that if my app was the last run application when I put the phone into standby (screen off), and I just press the Menu button to check the time, then the phone starts playing the game and sounds in the background (the app isn't actually visible, only the screen with the date/time is, yet onResume must have been called in my app). What am I to do here? Is there a way to discern what is reactivating the app, and then add a conditional statement that only starts the game when the app is actually visible?

Here is a snippet from my onResume:

@Override
    protected void onResume()
    {
        mySaveGame = Utilities.loadSavegame(this);

        //check the savegame
        if(mySaveGame!=null)
        {
            //start game using savegame values
            this.startFromSavedGame(mySaveGame.getIsLevelComplete());   
        }
        else
        {
            //run the 1st-run components
            this.startFirstRun();
        }

        super.onResume();
    }

The only thing I can think of doing to prevent the game from starting whenever the screen gets turned on (even when the app isn't visible) is to put this.finish() as the last line in onPause()... but that forces you to restart the app every time you want to go back to it because the precess itself was killed (which is fine because my onPause saves persistent data, but it's not an elegant solution).

Please help.

+4  A: 

Have you considered switching to onStart() and onStop(), rather than onResume() and onPause()?

CommonsWare
Thanks for replying. I have tried onStart()/onStop(), but get the same results... the game plays in the background even when not actually visible. According to the documentation, onStart() is called before onResume() (so that's why it doesn't work in my case) and onStop() "may never be called..."
borg17of20
Hmmmm... That sounds like the lock screen is not properly being considered as having the foreground. What device are you testing this on?
CommonsWare
I'm using a T-Mobile G1 (Android 1.6 stock firmware).
borg17of20
I can confirm that the T-Mobile G1 (Android 1.6) and the Nexus One (Android 2.1) will pause the foreground app when the screen is turned off and will resume that app when the lock screen appears after the screen is turned on. However, `onStop()` and `onStart()` are not called. Hence, you should change your code to use `onStop()` and `onStart()` rather than `onPause()` and `onResume()`.
CommonsWare
Okay, so I tried moving everything to onStart() again. That allows the lock screen to appear and not start the game in background (I don't know what I did on my first attempt with onStart() that made it produce the same thing as onResume()). I have a related issue now. After moving all the code to onStart(), and when resuming from the lock screen (pressing Menu twice), the game does not start... i.e. onStart() is never called again after the initial onCreate().
borg17of20
Yeah, there's no question the lock screen, as implemented, confounds matters. One workaround hack you could try is to `registerReceiver()` for `ACTION_SCREEN_OFF`. At this point, pop an `AlertDialog`, something to the effect of "Play suspended" with "resume" and "close" buttons. They tap "resume", and you fire the logic you ordinarily do now in `onStart()`. They tap "close", and you `finish()` your activity. Or something like that.
CommonsWare
Okay, I understand the logic behind what you suggest, but I've never tried to implement a BroadcastReceiver. I found this: http://stackoverflow.com/questions/1324307/how-to-use-an-intent-to-update-an-activity (I understand that example). Is that something like what you're suggesting? Thanks.
borg17of20
The accepted answer on that question is what I had in mind, yes.
CommonsWare
Okay, I'll try it out. Thanks for your help.
borg17of20