2
\$\begingroup\$

I searched in Unity Documentation but there are no information. I'd like to know which methods is called and what it returns in the following scenarios on Android:

  • App initially starts
  • App goes in the background
  • App is brought forward after being in the background

As for IOs I know that the scenarios should be the following:

  • App initially starts:

    OnApplicationFocus(true) is called

  • App goes in the background:

    OnApplicationFocus(false) is called

    OnApplicationPause(true) is called

  • App is brought forward after being in the background:

    OnApplicationPause(false) is called

    OnApplicationFocus(true) is called

\$\endgroup\$
2
\$\begingroup\$

The following snippet calls EnterApplication() and ExitApplication(), for you to implement yourself. It works in Editor, on Android, and on iOS, consistently. The methods are called when the application launches, backgrounds, resumes, and closes.

#if UNITY_IOS || UNITY_EDITOR
    private void OnApplicationFocus(bool focus)
    {
        if (focus)
        {
            EnterApplication();
        }
        else
        {
            ExitApplication();
        }
    }
#endif

#if UNITY_ANDROID || UNITY_EDITOR
    private void OnApplicationPause(bool pause)
    {
        if (pause)
        {
            ExitApplication();
        }
        else
        {
            EnterApplication();
        }
    }
#endif

If I remember right, in Editor, there is a minor inconsistency, although I cannot remember what. I wrote this code a while back, and thought it would be useful - although, I cannot exactly remember how it works.

\$\endgroup\$
0
\$\begingroup\$

In Android, there are Activities. When your app starts, some Activity is launched. You can get its state from few methods. In your case:

onCreate() - activity is just created

onPause() - another activity comes foreground

onResume() - user returns to activity

You can check Activity lifecycle in this image: http://1.bp.blogspot.com/-OqYcaNPtxmA/Ty09w4B2yiI/AAAAAAAAAQE/cnMwJxhklQU/s1600/activity_lifecycle.png

Note that android apps can have more than one activity. So for example onPause() doesn't always mean app went to background, since another activity could be launched and the first one is paused.

Docs:
https://developer.android.com/reference/android/app/Activity.html

https://developer.android.com/training/basics/activity-lifecycle/index.html

\$\endgroup\$
  • \$\begingroup\$ That's good to know! But do you know which method does unity return respectively when onCreate(), onPause() and onResume() are launched? For example, does onPause() call both OnApplicationFocus(false) and OnApplicationPause(true) as in the case of an app going in the background on iOS? \$\endgroup\$ – RationeUtor Jul 13 '16 at 19:43
  • 1
    \$\begingroup\$ I'm afraid I don't know the answer :/ \$\endgroup\$ – johnyX Jul 13 '16 at 19:56
  • \$\begingroup\$ For what I've found it seems that the only solution to know it is to check the Android log file and control when each of the function is called \$\endgroup\$ – RationeUtor Jul 14 '16 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.