Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6728763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:12:32+00:00 2026-05-26T10:12:32+00:00

We have an Android app that periodically polls for data and updates the display.

  • 0

We have an Android app that periodically polls for data and updates the display. The polling is performed by a background thread. Unfortunately, this background thread constantly runs, even when our app is offscreen or when the device is locked, which leads to unnecessary network activity and CPU/battery usage.

So, I would like to change the app such that it will suspend its polling activities in these cases:

  • When none of the application’s activities are in the foreground.
  • When the device is locked.

What is the easiest way to detect whether the app is in either of these states?

Note: The app has several activities, so I don’t think it is as simple as just keeping track of the activity lifecycle events. I would have to add code to each activity to keep track of whether any of my activities are in the foreground. If that’s what I need to do, I’ll do it, but I’m hoping there is a simpler way.


These are some questions which are related, but which don’t seem to provide a good answer:

  • Android: How to detect if current stack of activities (task) moves to background?
  • How to be notified about entering suspend mode?
  • Simple check for Android application backgrounding
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:12:33+00:00Added an answer on May 26, 2026 at 10:12 am

    What is the easiest way to detect whether the app is in either of these states?

    For the are-we-in-the-foreground issue, increment a reference count in a static data member in onStart() of each activity, and decrement it in onStop(). If onStop() sees 0, stop the polling. If onStart() sees that you’re not polling, start polling.

    For the is-the-screen-locked issue, don’t worry about it. The device will fall asleep once the screen times out and your polling thread will not be running. Besides, I think your activity will be stopped in this case anyway.

    BTW, this is one case where onStop() is the right answer, not onPause(), so the lifecycle handoff between your activities is handled properly.

    If that’s what I need to do, I’ll do it, but I’m hoping there is a simpler way.

    That’s as simple as it gets.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an android app that uses a background service for uploading data. When
I have this android app that posts (http post) some data strings to my
I have a android app that is quite simple it fetches data from the
I have android app that talks to server and syncs some data int SQLite
I have an android app that will record data every x mins. The time
I have a class that extends android.app.Dialog, the layout is done in an xml
I have an Android project that branched into three different applications, app-1 , app-2
I have an android app that repeatedly collects fingerprints from the wifi-networks that are
I have an Android app that in the onCreate() method, preloads a lot of
I have an Android app that saves a text file directly onto the phone,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.