Android/IOS application lifecycle

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By yonni
:warning: 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?

Thanks for your help and a great engine.

:bust_in_silhouette: Reply From: duke_meister

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.

yonni | 2016-06-01 23:08

Cool. It does what I need it for.

duke_meister | 2016-06-01 23:22