title: How to handle onPause/onResume in Flutter App?
body: <p>I'm new to Dart/Flutter and would like to build a simple app where a LinearProgressBar gets updated every second.</p>
  
  <p>Without getting too much into the actual code, I have the following setup working.</p>
  
  <ul>
  <li>A function that calculates the progress, based on passed time.</li>
  <li>A LinearProgressBar showing the progress.</li>
  <li>A periodic Timer recalculating the progress and updating the progress bar every second.</li>
  <li>I debugprint 'tick' every time, the recalculation is done.</li>
  </ul>
  
  <p>Everything is working as expected with one exception. The 'tick' keeps getting printed when I move the app in the background on my Android device.</p>
  
  <p>On native Android, I would cancel my periodic Timer when the 'onPause' event is triggered.</p>
  
  <p>Is there something similar in Flutter? All I could find was 'initState' and 'dispose'. Dispose, however, does not get called when moving the app to background.</p>
  
  <p>I don't want the timer to keep ticking in the background. </p>
  
  <p>On my research, I found this Stack Overflow question <a href="https://stackoverflow.com/questions/44331725/onresume-and-onpause-for-widgets-on-flutter">onresume-and-onpause-for-widgets-on-flutter</a>. It's answer suggests using <a href="https://docs.flutter.io/flutter/widgets/TickerProviderStateMixin-class.html" rel="noreferrer">TickerProviderStateMixin</a>.</p>
  
  <p>I used it the following way.</p>
  
  <pre><code>class _BarItemState extends State&lt;BarItem&gt; with SingleTickerProviderStateMixin {
      Ticker ticker;
      num progress = 1.0;
  
      @override
      void initState() {
         super.initState();
         ticker = createTicker((duration) =&gt; setState(() {
            debugPrint('tick');
            progress = duration.inSeconds / 30;
         }))
        ..start();
      }
  
      // other stuff omitted
  
  }
  </code></pre>
  
  <p>It is working, but I'm still not satisfied.</p>
  
  <p>The reason is, that the ticker callback is getting now called every few milliseconds instead of once a second. This seems to me like a waste of resources (I don't need a smooth animation), ... am I overcomplicating things? </p>
  
  <p>Even if it seems that I don't need it for my use case, I still would like to know: </p>
  
  <p>How to handle the onPause/onResume events on my own?</p>