Function onApplicationStateChanged