I'm trying to execute some function repeatedly for every 5 sec & it's working fine.

**I wanted this timer to stop when the app is closed or back button is pressed.**

```
  int delay = 0; 
        int period = 5000; // repeat every sec.
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                System.out.println("done");
            }
        }, delay, period);
    }

```