Android Events Question

Started by AGP, August 02, 2012, 07:58:26 AM

Previous topic - Next topic

AGP

I'm having trouble sending my app to the background and returning to it. My latest attempt looks like this:

In the loop (in Activity):

  public void run() {//THE GAMELOOP
    synchronized (pauseLock) {
    keepGoing = true;
    while (keepGoing) {
    if (paused) {
    try {pauseLock.wait();}
    catch (InterruptedException e) {}
    }
    ...
  }
THEN:
    protected void onPause() {
    if (musicOn)
    backMusic.pause();
    synchronized (pauseLock) {
    paused = true;
    //super.onPause();
    }
    }
    public void onResume() {
        synchronized (pauseLock) {
        pauseLock.notifyAll();
        super.onResume();
            if (keepGoing && paused) {
                paused = false;
            gameThread.resume();
            }
        }
    }


In short, this even broke the first time the program runs (it now runs properly the first time around SOMETIMES, and sometimes it doesn't). Sorry this isn't a jpct-specific question, but this is the best place I know in which to ask.

EgonOlsen

You actually don't have a game loop in Android. At least i don't. I'm doing all my game logic calls in onDrawFrame() which will be called by the system in fixed(?) intervalls anyway.

AGP

But what if I want one? How do I make it resume properly?

AGP

By the way, as this is not a jpct question, I'm using SurfaceView, not SurfaceViewGL: no onDrawFrame().

EgonOlsen

I see. What exactly happens if you try to do it that way?

AGP

#5
Sometimes it runs (Swype made my "runs" into "rings") the first time, sometimes it doesn't. But resuming never works.

EgonOlsen

Are you sure that your Activity survives the onPause() and the onResume() gets actually called? At least for Activities using OpenGL, Android tends to kill and recrate the Activity instead of doing a simple resume.

AGP

I'm not (apparently there are no guarantees either way). It was a multi-level game, anyway, so what I did was kill the gameloop on onPause(), save the level in volatile state with onSaveInstanceState(Bundle), then restart the thread on onResume() on the right level (the level starts over, but that's alright in this case).