Topic was automatically imported from the old Question2Answer platform.
Asked By
yonni
Old Version
Published before Godot 3 was released.
Is there a way to respond to mobile (or indeed, desktop) program lifecycle events such as the application being backgrounded/minimised or being auto-closed by the OS?
If not, where in the engine would I look to go about adding this functionality?
You can check for notifications such as quitting and focus changing but I don’t think there’s the full set of lifecycle events for Android (can’t help with iOS at all).
func _notification(what):
if what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST:
# quitting app or back-button on Android
save_game()
get_tree().quit()
if what == MainLoop.NOTIFICATION_WM_FOCUS_OUT && OS.get_name().nocasecmp_to("windows") != 0:
# switched to different app
save_game()
Amazing, this looks like just what I need. Let me just test it.
yonni | 2016-06-01 22:49
All works well. Just for completeness, on Android:
MainLoop.NOTIFICATION_WM_FOCUS_IN – is sent when you switch back to a running application (but not when it first opens seemingly)
MainLoop.NOTIFICATION_WM_FOCUS_OUT – is sent when you move out of the application by pressing the home button
MainLoop.NOTIFICATION_WM_QUIT_REQUEST – is sent when moving out of the application using the back button
MainLoop.NOTIFICATION_WM_UNFOCUS_REQUEST – is not used.