www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: AGP on August 02, 2012, 07:58:26 am

Title: Android Events Question
Post by: AGP on August 02, 2012, 07:58:26 am
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):
Code: [Select]
  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.
Title: Re: Android Events Question
Post by: EgonOlsen on August 02, 2012, 08:11:49 am
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.
Title: Re: Android Events Question
Post by: AGP on August 02, 2012, 08:14:41 am
But what if I want one? How do I make it resume properly?
Title: Re: Android Events Question
Post by: AGP on August 02, 2012, 09:01:35 am
By the way, as this is not a jpct question, I'm using SurfaceView, not SurfaceViewGL: no onDrawFrame().
Title: Re: Android Events Question
Post by: EgonOlsen on August 02, 2012, 09:40:03 am
I see. What exactly happens if you try to do it that way?
Title: Re: Android Events Question
Post by: AGP on August 02, 2012, 06:16:40 pm
Sometimes it runs (Swype made my "runs" into "rings") the first time, sometimes it doesn't. But resuming never works.
Title: Re: Android Events Question
Post by: EgonOlsen on August 03, 2012, 02:17:41 pm
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.
Title: Re: Android Events Question
Post by: AGP on August 06, 2012, 07:44:52 am
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).