diff --git a/native/cocos/platform/android/AndroidPlatform.cpp b/native/cocos/platform/android/AndroidPlatform.cpp index 58ea1f2e93..4d28969dd8 100644 --- a/native/cocos/platform/android/AndroidPlatform.cpp +++ b/native/cocos/platform/android/AndroidPlatform.cpp @@ -571,6 +571,10 @@ public: } // NOLINTNEXTLINE events::WindowDestroy::broadcast(ISystemWindow::mainWindowId); + + auto *windowMgr = _androidPlatform->getInterface(); + auto *window = static_cast(windowMgr->getWindow(ISystemWindow::mainWindowId)); + window->setWindowHandle(nullptr); break; } case APP_CMD_GAINED_FOCUS: diff --git a/native/cocos/platform/java/modules/SystemWindow.cpp b/native/cocos/platform/java/modules/SystemWindow.cpp index b2f37739f8..85dd115101 100644 --- a/native/cocos/platform/java/modules/SystemWindow.cpp +++ b/native/cocos/platform/java/modules/SystemWindow.cpp @@ -57,9 +57,8 @@ void SystemWindow::setWindowHandle(void *handle) { #if (CC_PLATFORM == CC_PLATFORM_ANDROID) //The getWindowHandle interface may have been called earlier, causing _handleMutex to be occupied all the time. bool lockSuccess = _handleMutex.try_lock(); - bool needNotify = _windowHandle == nullptr; _windowHandle = handle; - if (needNotify) { + if (!lockSuccess) { _windowHandlePromise.set_value(); } if (lockSuccess) { @@ -87,10 +86,13 @@ uintptr_t SystemWindow::getWindowHandle() const { SystemWindow::Size SystemWindow::getViewSize() const { #if (CC_PLATFORM == CC_PLATFORM_ANDROID) - CC_ASSERT(_windowHandle); - auto *nativeWindow = static_cast(_windowHandle); - return Size{static_cast(ANativeWindow_getWidth(nativeWindow)), - static_cast(ANativeWindow_getHeight(nativeWindow))}; + if (_windowHandle) { + auto *nativeWindow = static_cast(_windowHandle); + return Size{static_cast(ANativeWindow_getWidth(nativeWindow)), + static_cast(ANativeWindow_getHeight(nativeWindow))}; + } + // windowHandle may be nullptr when the surfaceView is destroyed. + return Size{static_cast(0), static_cast(0)}; #else return Size{static_cast(JNI_NATIVE_GLUE()->getWidth()), static_cast(JNI_NATIVE_GLUE()->getHeight())};