Question

I have kind of a tricky situation in my app. I implemented a mechanism that contacts a remote server every 60 seconds to check if new data is available. If so, the new data is displayed in the app. So far so good.

I implemented the mechanism the following way:

  • It gets started in the onCreate() method of my MainActivity.
  • The polling routine gets stopped in my MainActivity's onDestroy() method.

This makes sure the polling is always active as long as the app is running. Also, if I start another Activity from my MainActivity, I also want the polling to run.

My Problem:

When I exit my application via the Back-Button, everything works fine and the polling stops. But when I exit my application through the Home-Button, the polling routine stays alive since onDestroy() of my MainActivity is not called, but I want it to stop.

If I change my code and stop the polling routine in the onPause() method of my MainActivity, I have the problem that the polling also gets stopped when I launch a new Activity.

What I want:

I want the polling to run as long as my Application (not my MainActivity) is in foreground / visible to the user. As soon as the User exits the application by pressing the Home-Button from anywhere in the App, or by pressing the Back-Button from the MainActivity, I want the polling to stop.

ADDITIONAL:

I also do not want to restart and stop the service everytime I switch Activities. Since the user of my Application will switch Activitys very often, this would just be a lot of overhead. Furthermore, I want the "refresh"-cycle to be exactly 60 seconds. I cannot guarantee that when I always restart the service and stop it again. It needs to be started once when the app gets started, and stopped when the app is no longer in foreground.

How to achieve that?

Isn't there some kind of simple way to check when the App is in foreground and when its hidden / closed?

This is my singelton-polling mechanism:

private Timer t;
private static Updater instance;

protected Updater() {
      // Exists only to defeat instantiation.
}

public static Updater getInstance() {

    if(instance == null) {
        instance = new Updater();
    }

    return instance;
}

public void startPollingRoutine(int interval, int delay) {      

    t = new Timer();

    // Set the schedule function and rate
    t.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {

            update(); // some asynctask that does the updating
        }

    }, delay, interval);
}

public static void stopPollingRoutine() {
    t.cancel();
}

In code:

Updater.getInstance().startPollingRoutine(60000, 0);
Updater.getInstance().stopPollingRoutine();
Was it helpful?

Solution 4

Thank you all for your help and time. I finally found a solution by myself that meets all my requirements.

I followed this tutorial to make it work: http://www.mjbshaw.com/2012/12/determining-if-your-android-application.html

The basic Idea is to extend the Application class and keep reference to how many Activitys are "alive" at a certain time.

OTHER TIPS

I think the best way how to handle it is to create some BaseActivity which all activities will extend. And to perform this actions in onResume/onPause. Or you can try using services.

you need to create a service and trigger it in your main class that service will be triggered unless you explicitly stop it on destroy means onBackpressed()/onDetroy() and let it run in onPause() method

Use a singleton, and let it have a counter variable. Increase it when you send intent to start a new activity, and decrease it in the onPause. Than you can tell if the polling has to stop; when the counter in the singleton is zero.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top