Multiple instances of android application
-
I am trying to write an android application that opens PDF files (that is, it responds to PDF viewing intents). However, when I open the application while another instance is already open by clicking on a PDF file, the application crashes. It appears that the android version of Qt is not capable of handling multiple instances?
Here is a minimal reproducible code sample:main.cpp
file:#include <QApplication> #include <QPushButton> int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget container; QPushButton button("Click me", &container); container.show(); return a.exec(); }
AndroidManifest.xml
file. This is the same as the default autogenerated manifest file by qt, except the intent-filters for PDF files:<?xml version="1.0"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.qtproject.example" android:installLocation="auto" android:versionCode="-- %%INSERT_VERSION_CODE%% --" android:versionName="-- %%INSERT_VERSION_NAME%% --"> <!-- %%INSERT_PERMISSIONS --> <!-- %%INSERT_FEATURES --> <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" /> <application android:name="org.qtproject.qt.android.bindings.QtApplication" android:hardwareAccelerated="true" android:label="-- %%INSERT_APP_NAME%% --" android:requestLegacyExternalStorage="true" android:allowNativeHeapPointerTagging="false" android:allowBackup="true" android:fullBackupOnly="false"> <activity android:name="org.qtproject.qt.android.bindings.QtActivity" android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation|mcc|mnc|density" android:label="-- %%INSERT_APP_NAME%% --" android:launchMode="singleTop" android:screenOrientation="unspecified" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter > <action android:name="android.intent.action.OPEN_DOCUMENT" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/pdf" /> </intent-filter> <intent-filter > <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/pdf" /> </intent-filter> <meta-data android:name="android.app.lib_name" android:value="-- %%INSERT_APP_LIB_NAME%% --" /> <meta-data android:name="android.app.arguments" android:value="-- %%INSERT_APP_ARGUMENTS%% --" /> <meta-data android:name="android.app.extract_android_style" android:value="minimal" /> </activity> </application> </manifest>
If you open the app and then try to open another instance by clicking on a PDF file, it freezes and the only way to open it again is to close it from system menu. This blog post suggests setting
android:launchMode:"singleInstance"
in the manifest file, but it is only a partial solution, because even with that when opening a PDF file from another application (e.g. google drive or telegram) still a new version of the application is created and it still crashes. -
Should I open a feature request for this? Even the author of the previous blog mentions this in the next part of the series here:
If the App was open, onNewIntent() from our customized Activity should be called, but Google Docs always calls onCreate() - doesn‘t matter if the Target App is already running or not. Then a white screen comes up, the App hangs and must be closed. The Debug Log reports:
E Qt JAVA : Surface 2 not found!
I haven‘t found a way to workaround this yet - perhaps anyone else has an idea HowTo fix ?
That blog post was from 2018! I find it very surprising that there doesn't seem to be any discussion on this issue on the web. It seems like a pretty huge problem. Am I doing something wrong maybe?
-
@hexo I have a small test program with poppler on Ubuntu.
it works like the following:auto new_document = Poppler::Document::load( file_name ); if ( nullptr == new_document ) { /* check open is ok first */ std::cout << " can not open pdf file " << qPrintable( file_name ) <<std::endl; return; } closeDocument(); /* close it for other apps */ //use new_document
no crash.
-
This post is deleted!