# How do I know when the app is launched, loses focus and gets focus again on mobile?

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

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.

• Check out that platform-independence. Gotta wonder why they called it "Unity" sometimes. May 28 '21 at 22:31

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:

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.

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

• 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? Jul 13 '16 at 19:43
• I'm afraid I don't know the answer :/ Jul 13 '16 at 19:56
• 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 Jul 14 '16 at 10:51

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.