DEV Community

KENTO⚽️XR Engineer😎
KENTO⚽️XR Engineer😎

Posted on • Updated on

【Unity】 A solution if you can't receive the event that finishes an app on mobile.

Intro

I tried to receive the app's end event on mobile.
However, it did not succeed.

So I have considered a compromise. I'll write it down.

Environments

name version
Unity 2019.4.23f1

Problem

Unity has a solution to receive the app's end event.
It is OnApplicationQuit.

But it doesn't work on mobile.
OnApplicationQuit will only be called in situations that you explicitly call Application.Quit.

You can use OnApplicationFocus,but It is also called when working in the background.

Solution

I have come up with one solution.

First, I tried to figure out why I would want to receive the app's end event.

In my case, I wanted to delete the cache of streaming files.
Therefore, I changed the implementation to delete a files when I start an app.

//Call on start an app. 
void Start()
{
    try
    {
        if (Directory.Exists(Path))
        {
            Directory.Delete(Path, true);
        }
        Debug.Log("Cache delete");
    }
    catch
    {
        Debug.LogError("Error deleting files.");
    }
}
Enter fullscreen mode Exit fullscreen mode

It is my compromise solution.

If you want to send game scores and statuses at the end of the app, you may want to save the data when the user takes some action and send it at startup.

Other Resources

OnDisable and OnDestroy not Called on iOS

Top comments (0)