Saving frequently throughout game lifecycle (Android)

I’m working on a mobile game that saves throughout the game. Namely, I want it to save the game data whenever the app is closed completely and when the app goes out of focus. I’d like to have the game load the save data when the app is opened and when it comes back into focus. I have a rudimentary Save/Load system that I built following one of the live training tutorials, but I’m stuck.

My main question is: where do I call the Save and Load methods in the lifecycle so that it saves and loads when I want it to as described above? I know I can use Start, but what method is the best for when the app comes back into focus or when it leaves focus/closes?


In other words, can I have unity take actions during a specific point in the Android activity lifecycle?

Add this script to your edit folder, It saves every 5 minuets. You can change the time in the script


using UnityEngine;
using UnityEditor;

 [InitializeOnLoad]
 public class AutosaveOnRun
 {
     static AutosaveOnRun()
     {
         EditorApplication.playmodeStateChanged = () =>
        {
         if(EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying)
         {
             Debug.Log("Auto-Saving scene before entering Play mode: " + EditorApplication.currentScene);
             
             EditorApplication.SaveScene();
             EditorApplication.SaveAssets();
         }
     };
 }

}