Skip to content

destroy ​

Destroy the Mini-Game Runtime Environment ​

destroy() is the endpoint of the entire lifecycle. It is used to completely shut down the game engine and reclaim all occupied system resources (including JS virtual machine memory, texture resources, thread pools, and the View handle). After calling this method, the mini-game instance returns to its initial unavailable state.


Method Declaration ​

java
/**
 * Destroys the mini-game runtime environment.
 * On success, the mini-game instance enters the `UNAVAILABLE` state.
 * After the call, all resources bound to the current handle are released, and the View becomes invalid.
 */
void destroy();

Runtime Behavior and State Transitions ​

1. Preconditions ​

  • Can be called in any of the WAITING, RUNNING, or PLAYING states.
  • It is recommended to call stop() first to ensure the game logic has gracefully triggered onHide.

2. Execution Process ​

  • Engine unloading: Shuts down the built-in JS engine (V8/JSC) and clears all script contexts.
  • Memory release: Destroys the WebGL/Canvas rendering surface and releases image and model data from video memory.
  • Listener unbinding: Automatically unbinds the various state and custom command listeners set previously.

3. Result ​

  • On success, the state changes to GAME_STATE_UNAVAILABLE.
  • At this point, the View returned by getGameView() and its content are destroyed and can no longer be added to the UI tree.

Example Code ​

java
@Override
protected void onDestroy() {
    if (gameHandle != null) {
        // 1. Stop running (optional, depending on whether the game's internal save logic needs to be triggered)
        gameHandle.stop("{}");
        
        // 2. Completely destroy the environment
        gameHandle.destroy();
        
        // 3. Set the handle to null to prevent subsequent misuse
        gameHandle = null;
    }
    super.onDestroy();
}

Notes ​

  • Irreversibility: Once destroy() is executed, this gameHandle instance has fulfilled its purpose. To play the game again, you must restart the full flow from setGameStartOptions and create().
  • Memory leak prevention: Be sure to call this method when the Activity is destroyed. Failing to destroy may cause the JS engine and rendering surface to lead to serious memory leaks.
  • Thread safety: destroy() is a heavyweight synchronous operation. It is recommended to execute it immediately once you confirm the game screen is no longer needed (such as the moment the Activity exits).