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\$

3 Answers 3

4
\$\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\$
1
  • 1
    \$\begingroup\$ Check out that platform-independence. Gotta wonder why they called it "Unity" sometimes. \$\endgroup\$
    – Jason C
    Commented May 28, 2021 at 22:31
1
\$\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\$
3
  • \$\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\$ Commented Jul 13, 2016 at 19:43
  • 1
    \$\begingroup\$ I'm afraid I don't know the answer :/ \$\endgroup\$
    – johnyX
    Commented Jul 13, 2016 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\$ Commented Jul 14, 2016 at 10:51
0
\$\begingroup\$

Ok; I've never done Oculus development but I stumbled upon their Unity lifecycle documentation while researching the same question, figured I'd post it here as one more step towards solving the mystery. From there (see original page for full details; I'm intentionally filtering this down to standard Unity messages only):

HMD unmount and mount with a delay

When a user removes the HMD, then puts it back on at a later time (~10 seconds or more):

  • When HMD is removed
    • ... after ~10 seconds: OnApplicationPause(true)
  • When HMD is replaced
    • OnApplicationPause(false)

HMD exits Guardian tracked space

The user’s HMD exits the defined play area

  • OnApplicationFocus(false)
  • OnApplicationPause(true)

HMD returns to play area

  • OnApplicationFocus(true)
  • OnApplicationPause(false)
  • ...

Power button is pressed to turn off the display

  • OnApplicationPause(true)

Power button is pressed to turn on the display

  • OnApplicationPause(false)
  • ...

Oculus button is pressed to open the System UI

  • OnApplicationFocus(false)
  • OnApplicationPause(true)

User selects “resume”

  • OnApplicationFocus(true)
  • OnApplicationPause(false)

Also, it might be worth noting that that page has an "Application Start" section that doesn't mention any of the standard messages, so I'm not sure if that's a documentation error or if focus / pause events just aren't sent initially:

Application start

When the application is initialized -

  • HMDAcquired
  • HMDMounted
  • VrFocusAcquired
  • TrackingAcquired

Both the Rift and the Quest have the same behavior, according to those docs. I do not, however, know if any of that is also covered by the new XR plugin system for AR/VR platforms.

Hopefully that helps.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .