QtOnAndroid

Life Cycle

Using JNI to show an Android notification | | SQLite, OpenGL ES, etc…

The main restriction of Android is that only a single app is guaranteed to run continuously in the foregroud. Any other app that has been sent to the background can be killed by the Android OS at any time due to scarce resources or incoming phone calls.

Whenever an app in the backgroud is killed, it needs to save its applications state, so that it is able to restart the activity where is was stopped. In the app this is done in the onDestroyed() and onResumed() Java methods.

So a Android Java app cyles through the following states:

running → paused → destroyed → resumed

For an overview of the app life cycle see the Android developer documentation.

To receive updates on the application status in Qt, we need to connect to the applicationStateChanged signal:


   // connect status change signal
   connect(qApp, SIGNAL(applicationStateChanged(Qt::ApplicationState)),
           this, SLOT(changed_app_status()));
 

void changed_app_status()
{
   if (qApp->applicationState() == Qt::ApplicationActive)
   {
      qDebug() << "app is active"
   }
   else
   {
      qDebug() << "app has been deactivated"
   }
}
 

In Qt saving the application state in persistent memory is done via the QSettings class.

Making a setting permanent is done as follows:

{
   QSettings prefs;
   prefs.setValue("name", value);
}

If a particular setting is of the type QString, we can read its value back with:

{
   QSettings prefs;
   if (prefs.contains("name")) value = prefs.value("name").toString();
}


Using JNI to show an Android notification | | SQLite, OpenGL ES, etc…

Options: