Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by trs9556 · May 04, 2013 at 03:24 AM · androidbackgroundhome button

How to tell if your android app has been put in the background?

Hello,

I keep track of some statistics on my app, like run time, what buttons have been pressed etc. I would like to know is there any way to tell if my app has been put in the background (aka if the user hits the home button). I don't wish to override it by any means I just would like to be able to send that data to my server and stop tracking it when the user does that, before android automatically closes out my app and destroys all that data.

I tried: void OnApplicationPause(bool pauseStatus)

and that works fine in the editor, but after running some test it doesn't get called on android after you hit the home button.

Thanks.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by trs9556 · May 04, 2013 at 04:39 AM

Ah-HA!

Using

 void OnApplicationFocus(bool pauseStatus) {
     if(pauseStatus){
         //your app is in the background, yay!!!!
     }
 }

It appears to be a good way to detect if your app goes into the background aka loses focus.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image danielng01 · Jun 28, 2014 at 11:55 AM 0
Share

thanks a lot

avatar image errorage · Jan 27, 2016 at 01:44 PM 4
Share

Correct method, incorrect use of the attribute. In your case, pauseStatus will be true when your app is no longer in the background

 void OnApplicationFocus(bool pauseStatus) {
      if(pauseStatus){
          //your app is NO LONGER in the background
      }else{
          //your app is now in the background
      }
 }
avatar image DevSpur errorage · Jul 07, 2017 at 04:21 PM 0
Share

Thanks this works really well

avatar image c8theino · Feb 24 at 12:21 PM 0
Share

This seems to be called when the app is moving to background, even though the app is still playing. For example in Android 11 -> when you start moving the app to the background it is called instantly. If you want to know when the app is completely moved to the background so that the app is also paused, you should use this function instead:

 private void OnApplicationPause(bool pause) {
     if (pause) {
         // App is paused
     } else {
         // App resumed
     }
  }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Android] Call Home button action from code 0 Answers

Could a mobile app have a service application compiled targetting a higher version of .net? 0 Answers

Run Unity as a background widget on Mobile devices. 0 Answers

When backing out of Game on Android(h/w home button), and returns to play.. touch controls no longer work?(solved) 1 Answer

2D - draw sprite only on one background color 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges