From d8570cb1a85baa1d210c62e705166458f8f83718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tinja=20Paavosepp=C3=A4?= Date: Tue, 8 Oct 2024 14:39:49 +0300 Subject: [PATCH] QtQuick for Android: Do not terminate Qt during configuration change In Android the default behavior is that when a configuration change, e.g. an orientation change, happens, the Activity is destroyed and a new one is created with the new configuration. In regular Qt for Android, this is not a problem as the QtActivity overrides this behavior and swicthes the configuration without recreating the Activity. However in QtQuick for Android i.e. when adding a QtQuickView to an otherwise regular Android app, we cannot rely on that being the case. Currently, if for example the orientation is changed with an Activity that has a QtQuickView in it and the default behavior is used, it will crash, as the destruction of the Activity will terminate the Qt runtime, and whne creating it anew the runtime is not properly restarted. This patch does not fix the underlying issue of not restaring the Qt runtime properly, but it adds a check so we don't terminate it when the reason for the Activity destruction is a configuration change, as in those cases it will anyway be recreated immediately, meaning a full termination is too heavyhanded anyway. Task-number: QTBUG-123711 Change-Id: Ia44ab8f55855aa5d54c5fb25e3cca90a9c0feaaa Reviewed-by: Assam Boudjelthia (cherry picked from commit aef9fff1d7a0de98cb446a634fdf2812e45f19c8) Reviewed-by: Qt Cherry-pick Bot --- .../jar/src/org/qtproject/qt/android/QtEmbeddedDelegate.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/android/jar/src/org/qtproject/qt/android/QtEmbeddedDelegate.java b/src/android/jar/src/org/qtproject/qt/android/QtEmbeddedDelegate.java index 3c8265f0e0a..8b1c29e2e54 100644 --- a/src/android/jar/src/org/qtproject/qt/android/QtEmbeddedDelegate.java +++ b/src/android/jar/src/org/qtproject/qt/android/QtEmbeddedDelegate.java @@ -75,7 +75,10 @@ class QtEmbeddedDelegate extends QtActivityDelegateBase @Override public void onActivityDestroyed(Activity activity) { - if (m_activity == activity && m_stateDetails.isStarted) { + // If the Activity was destroyed due to a configuration change, it will be recreated + // instantly, so don't terminate Qt if that's the case + if (m_activity == activity && m_stateDetails.isStarted && + !activity.isChangingConfigurations()) { m_activity.getApplication().unregisterActivityLifecycleCallbacks(this); QtNative.unregisterAppStateListener(QtEmbeddedDelegate.this); QtEmbeddedViewInterfaceFactory.remove(m_activity);