title: Xamarin ContentView/View Lifecycle
body: <h1>Version</h1>
  <p>Xamarin.Android 8.2</p>
  <p>Xamarin.Forms 2.5</p>
  <h1>Short question</h1>
  <p>How do I, or should I let a ContentView know the life cycle state of its containing Page (e.g. Appearing, Disappearing, Sleeping)</p>
  <h1>Long question</h1>
  <p>I am learning how to create a reusable Xamarin ContentView. I decide to create a control <code>&lt;LabelCarousel/&gt;</code> that will display its children label one after another.
  The problem is I have no idea how to stop the background timer that switches the content</p>
  <h2>Sample</h2>
  <p><a href="https://i.stack.imgur.com/V1FJy.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/V1FJy.gif" alt="Sample" /></a></p>
  <h2>Usage</h2>
  <pre><code>&lt;local:LabelCarousel&gt;
      &lt;Label&gt;Hello&lt;/Label&gt;
      &lt;Label&gt;Hola&lt;/Label&gt;
  &lt;/local:LabelCarousel&gt;
  </code></pre>
  <h2>Implementation</h2>
  <pre><code>namespace XamarinStart.Views
  {
      [XamlCompilation(XamlCompilationOptions.Compile)]
      [ContentProperty(&quot;LabelContainer&quot;)]
      public partial class LabelCarousel : ContentView
      {
          [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
          [Description(&quot;Labels&quot;), Category(&quot;Data&quot;)]
          public List&lt;Element&gt; LabelContainer { get; } = new List&lt;Element&gt;();
  
          private int _index = 0;
          private Timer _timer = new Timer(2000);
  
          public LabelCarousel ()
          {
              InitializeComponent ();
  
              _timer.Elapsed += TimerEvent;
              _timer.Start();
              
          }
  
          private void TimerEvent(object sender, ElapsedEventArgs e)
          {
              if (_index &gt;= LabelContainer.Count)
              {
                  _index = 0;
              }
  
              var selected = LabelContainer[_index++];
              ChangeLabel((Label)selected);
          }
  
          private void ChangeLabel(Label lbl)
          {
              Device.BeginInvokeOnMainThread(async () =&gt;
              {
                  await this.FadeTo(0);
                  Content = lbl;
                  await this.FadeTo(1);
              });
          }
      }
  }
  </code></pre>
  <h2>Problem</h2>
  <p>When I put this app into background, or push another activity on the top of this one, the timer is still running, wasting the CPU resource. Is there any good idea to let the reusable ContentView notified when the parent page changes the state?</p>
  <h2>Related Posts</h2>
  <p><a href="https://forums.xamarin.com/discussion/38989/contentview-lifecycle" rel="nofollow noreferrer">https://forums.xamarin.com/discussion/38989/contentview-lifecycle</a>
  <a href="https://forums.xamarin.com/discussion/65140/we-need-a-way-for-contentview-and-viewcells-to-monitor-their-own-lifecycle" rel="nofollow noreferrer">https://forums.xamarin.com/discussion/65140/we-need-a-way-for-contentview-and-viewcells-to-monitor-their-own-lifecycle</a></p>
  <p>Thank you!</p>