Sketch Freezes/Stops When Going To Home And Lock Screen

Hello everyone,

I was wondering if your android app sketch freezes when you go to the home screen and then go back to your app without fully closing and reopening the app. Does it freeze when you are on your app and you turn your phone off? I am using an amazon fire tablet and my sketch always freezes in these situations, but if I wait about a minute or two, it starts playing again. Below is a sample bit of code that I was using to observe this behavior. Does anyone have a solution so that the sketch starts playing immediately after resuming the app? If I have to change something in android studio, that is fine. I appreciate all the help in advanced!

int BallX,BallY;
int BallDX = 5, BallDY = 5;

void setup() {
  fullScreen(P2D);
  BallX = width/2;
  BallY = height/2;
  fill(255);
}

void draw() {
  background(100,255,100);
  circle(BallX,BallY,100);
  BallX += BallDX;
  BallY += BallDY;
  if (BallX < 50 || BallX > width-50) {
    BallDX *= -1;
  }
  if (BallY < 50 || BallY > height-50) {
    BallDY *= -1;
  }
}

Using APDE I never noticed this behavior.
And using the life-cycle functions I can catch the variables to start exactly where it paused without problems. Did you try APDE?

@Lucas === does this happens with all apps even the most simple one (draw an ellipse or something like that)?- If yes it is weird (perhaps related to your device), if no try to use on pause() && onResume() to solve that.

1 Like

@akenaton
Even drawing only an ellipse froze my sketch, but after adding the onPause() and onResume(), it worked perfectly! Thank you!

Here is the new code, it is the same but has the two new functions at the end:

int BallX,BallY;
int BallDX = 5, BallDY = 5;

void setup() {
  fullScreen(P2D);
  BallX = width/2;
  BallY = height/2;
  fill(255);
  textAlign(CENTER);
  textSize(100);
}

void draw() {
  background(100,255,100);
  circle(BallX,BallY,100);
  BallX += BallDX;
  BallY += BallDY;
  if (BallX < 50 || BallX > width-50) {
    BallDX *= -1;
  }
  if (BallY < 50 || BallY > height-50) {
    BallDY *= -1;
  }
  text(frameRate,width/2,height/2);
}

void onPause() {
}

void onResume() {
}