title: Terminating Android Timer if user doesn't use the App(Prevent running in the background)
body: <p>I have a <code>Timer</code> in my App that infinitely runs an <code>Animation</code>. like this:</p>
  
  <pre><code>Timer t = new Timer();
  t.scheduleAtFixedRate(new TimerTask() {
  
      @Override
      public void run() {
          runOnUiThread(new Runnable() {
  
              @Override
              public void run() {
                  //Running Animation Code
              }
          });
      }
  }, 1000, 1000);
  </code></pre>
  
  <p>Now I realized that this code runs even if user click <code>Back Button</code> of android. if fact it runs in the background and it seems uses a lot of memory.</p>
  
  <p>I need this code run <code>ONLY if user in the app</code>. In fact when user click on <code>Back Button</code>, this <code>Timer</code> goes to end and if user clicks on <code>Home Button</code>, after a while that user doesn't use the App, terminates this <code>Timer</code>.</p>
  
  <p>What I need is to prevent using memory. Because i realized if this codes runs a while, App freezes! I need a normal behavior.</p>