Use the applicationStateChanged signal.