From 08a4fd357064cfefd8de7af13025a9f1cb04cafb Mon Sep 17 00:00:00 2001 From: Samuel Mira Date: Wed, 27 Apr 2022 22:57:02 +0300 Subject: [PATCH] Android: Fix ANR when QtService and QtActivity load in same process This patch is a fix to prevent the ANR. In this patch, the QtService queries QtNative if there is a QtActivity or QtService started and, in case there is, it does not begin the loading process. When that happens, the QtService will be a regular android Service. Fixes: QTBUG-99691 Pick-to: 5.15 6.2 6.3 Change-Id: Ibd8aa8554107a9744b53cca4e0dd7e6f9b25baea Reviewed-by: Assam Boudjelthia Reviewed-by: Nicholas Bennett --- .../qtproject/qt/android/bindings/QtService.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/android/java/src/org/qtproject/qt/android/bindings/QtService.java b/src/android/java/src/org/qtproject/qt/android/bindings/QtService.java index afbe4350a0..06616391f0 100644 --- a/src/android/java/src/org/qtproject/qt/android/bindings/QtService.java +++ b/src/android/java/src/org/qtproject/qt/android/bindings/QtService.java @@ -37,6 +37,7 @@ package org.qtproject.qt.android.bindings; import android.app.Service; +import android.util.Log; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; @@ -54,7 +55,17 @@ public class QtService extends Service /////////////// PLEASE DO NOT CHANGE THE FOLLOWING CODE ////////////////////////// ////////////////////////////////////////////////////////////////////////////////// protected void onCreateHook() { - m_loader.onCreate(); + // the application has already started + // do not reload everything again + if (QtNative.isStarted()) { + m_loader = null; + Log.w(QtNative.QtTAG, + "A QtService tried to start in the same process as an initiated " + + "QtActivity. That is not supported. This results in the service " + + "functioning as an Android Service detached from Qt."); + } else { + m_loader.onCreate(); + } } @Override public void onCreate()