Implemented visibilitychange on the main loop