In this article, we'll show you how to tell if a QML app is pushed to the background or foreground. We know that in the Ubuntu mobile platform, it is a single application operating system. When an app is pushed to the background, the app is suspended and cannot be run. We sometimes need this logo to determine when our app is in the foreground and when it's in the background.
We use the Ubuntu SDK to create a simple QML application:
Import QtQuick 2.0import ubuntu.components 1.1/*! \brief MainView with a Label and Button elements.*/mainview { id:main //ObjectName for functional testing Purpos ES (AUTOPILOT-QT5) objectName: "MainView" //note! ApplicationName needs to match the "name" field of the the Click Ma Nifest ApplicationName: "Com.ubuntu.developer.liu-xiao-guo.foregrounddetect"/* This property enables The application to change orientation if the device is rotated. The default is False. */ //automaticorientation:true //Removes the old toolbar and enables new features of the new header. Usedeprecatedtoolbar:false Width:units.gu (height:units.gu) page { title:i18n.tr (" Foregrounddetect ") Connections { target:Qt.application onactivechanged: { Console.log (" Qt.application.active: "+ Qt.application.active);}}}}
Here, we use the "active" property of the variable qt.application to determine whether an app is pushed to the background or foreground. We run the application with the following results:
When we push the app to the background on the phone, false is displayed and true when we push the app to the foreground.
The source of the entire application is:BZR branch lp:~liu-xiao-guo/debiantrial/foregrounddetect
How to tell if the Qml app is pushed to the background and foreground