The implementation can be found in the guide I mentioned in the beginning. It looks like this:
MainWindow &MainWindow::instance(QWidget *parent)
{
static MainWindow mainWindow(parent);
return mainWindow;
}
There is also explained, why it is a little bit more difficult then one would expect.
The problem is that the function in native.cpp is executed in Android UI thread and I need to call the function in AndroidLib so that it is called in Qt main loop.
After many trial and errors I finally made it to work although I'm not sure why. I found out that if I called the function in AndroidLib directly from main.cpp. Then it was executed. So I implemented a signal and connected it to the class when I needed the information. But that didn'T work.
Now I've used this in my code:
QObject::connect(&AndroidLib::instance(), SIGNAL(sdMount(bool)), qmltocpp, SLOT(sdMount(bool)));
And it works!!!! The function in AndroidLib is called, the signal is emitted and received in my qmltocpp class. Because C++ doesn't have a garbage collector, I hope the AndroidLib instance will not be destroyed. And I can always get the reference to it by calling the static instance() function. The constructor is now a private function as in the example. Maybe that was the trick, I'm really confused.
Now I need to move it to a service. That will be the next task. So the problem is solved but I still have no idea why it didn't work earlier and now it works.