Author Topic: Android Events Question  (Read 2724 times)

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Android Events Question
« 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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Android Events Question
« Reply #1 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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Android Events Question
« Reply #2 on: August 02, 2012, 08:14:41 am »
But what if I want one? How do I make it resume properly?

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Android Events Question
« Reply #3 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().

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Android Events Question
« Reply #4 on: August 02, 2012, 09:40:03 am »
I see. What exactly happens if you try to do it that way?

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Android Events Question
« Reply #5 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.
« Last Edit: August 02, 2012, 11:23:23 pm by AGP »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Android Events Question
« Reply #6 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.

Offline AGP

  • quad
  • ******
  • Posts: 1726
    • View Profile
Re: Android Events Question
« Reply #7 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).