Skip to content

Commit

Permalink
[android] Fixes #2682, remove frame callback if view is no longer att…
Browse files Browse the repository at this point in the history
…ached to window.
  • Loading branch information
badlogic committed Jan 22, 2025
1 parent d3f7483 commit 8c37b4c
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ public SpineView build () {
private Alignment alignment = Alignment.CENTER;
private ContentMode contentMode = ContentMode.FIT;

private boolean isAttached = false;

/** Constructs a new {@link SpineView}.
*
* After initialization is complete, the provided {@code SpineController} is invoked as per the {@link SpineController}
Expand Down Expand Up @@ -208,6 +210,18 @@ public SpineView (Context context, AttributeSet attrs, int defStyle) {
// Set properties by view id
}

@Override
protected void onAttachedToWindow () {
super.onAttachedToWindow();
isAttached = true;
}

@Override
protected void onDetachedFromWindow () {
super.onDetachedFromWindow();
isAttached = false;
}

/** Constructs a new {@link SpineView} from files in your app assets. The {@code atlasFileName} specifies the `.atlas` file to
* be loaded for the images used to render the skeleton. The {@code skeletonFileName} specifies either a Skeleton `.json` or
* `.skel` file containing the skeleton data.
Expand Down Expand Up @@ -407,10 +421,9 @@ private void updateCanvasTransform () {
controller.setCoordinateTransform(x + offsetX / scaleX, y + offsetY / scaleY, scaleX, scaleY);
}

// Choreographer.FrameCallback

@Override
public void doFrame (long frameTimeNanos) {
if (!isAttached) return;
if (lastTime != 0) delta = (frameTimeNanos - lastTime) / 1e9f;
lastTime = frameTimeNanos;
invalidate();
Expand Down

0 comments on commit 8c37b4c

Please sign in to comment.