title: Xamarin. Different behavior app after resume
body: <p>For my first project during learning xamarin I make simple app where user can create a note and add alarm time to schedule local notification.
  I have a problem when app resume from background.</p>
  
  <p>To the point.</p>
  
  <p><strong>Note model:</strong></p>
  
  <pre><code>public class Note
  {
      [PrimaryKey, AutoIncrement]
      public int Id { get; set; }
      [MaxLength(255)]
      public string Title { get; set; }
      [MaxLength(255)]
      public string Content { get; set; }
  
      public DateTime TimeCreate { get; set; }
      public DateTime AlarmTime { get; set; }
      public bool AlarmTimeActive { get; set; }
  }
  </code></pre>
  
  <p>In main page there is list of notes. Every note has a switch button where user can on/off time alarm.
  If user try to switch on alarm, function check is schedule time gone or not. If gone then switch stay in off position and app display information. In other case function updates value in data base to "true".</p>
  
  <p><strong>XAML</strong></p>
  
  <pre><code>&lt;local:ButtonActiveSwitcher Toggled="Switch_Toggled" IsToggled="{Binding AlarmTimeActive}" Active="{Binding .}" /&gt;
  </code></pre>
  
  <p><strong>function "Switch_Toggled"</strong></p>
  
  <pre><code>private void Switch_Toggled(object sender, ToggledEventArgs e)
  {    
      var switchBtn = sender as Switch;
  
      var item = ((ButtonActiveSwitcher)sender).Active;          
      if (item != null)
      {
          if (item.AlarmTime &lt; DateTime.Now)
          {
              if (_nooLoopTime.AddSeconds(2) &lt; DateTime.Now) //Prevent double display alert
              {
                  DisplayAlert("ALERT", "Time gone", "OK");
                  _nooLoopTime = DateTime.Now;
              }                    
              switchBtn.IsToggled = false;
              return;
          }                
          DataBaseService.updateRecord(item);
      }           
  }
  </code></pre>
  
  <p>And this function works fine when user tapped switcher.</p>
  
  <p>Next point.</p>
  
  <p>In <em>MainPage.cs</em> in function <code>OnAppearing</code> app fired function <code>DataBaseService.checkNoteAlarmTimeActive();</code>. In this function app check <code>AlarmTime</code> in notes. If <code>AlarmTimeActive</code> is active but schedule time has gone then change <code>AlarmTimeActive</code> to "false".</p>
  
  <p>First app checks Notes in DB and update them, next function <code>loadNotes()</code> getting Notes from DB and populate list.</p>
  
  <p>So before app gets Notes from DB first update records in DB.</p>
  
  <p><strong>MainPage.cs</strong></p>
  
  <pre><code>[XamlCompilation(XamlCompilationOptions.Compile)]
  public partial class MainPage : ContentPage
  {
      private Sorting _sorting;
      private int _sortOption;
      private bool _activeSwitcherState;
      private DateTime _nooLoopTime;
  
  
      public MainPage(int noteid)
      {
          DataBaseService.CreateTables();            
          this._sorting = new Sorting();                     
  
          InitializeComponent();
          this.AddPickerItems();
          if (noteid != 0)
          {
              this.loadNoteView(noteid);
          }
      }
  
      protected async override void OnAppearing()
      {          
          await DataBaseService.checkNoteAlarmTimeActive();
          this.loadNotes();                       
          base.OnAppearing();
      }
  
      /// &lt;summary&gt;
      /// Generate list of notes basic on current sorting option and active switcher
      /// &lt;/summary&gt;     
      private async void loadNotes()
      {
          listNotes.ItemsSource = await _sorting.sortNotes(_sortOption, _activeSwitcherState);          
      }
  }
  </code></pre>
  
  <p>And here is my problem.</p>
  
  <p>For example: one Note has <code>AlarmTimeActive</code> "true" and user tapped "Home" button, app goes to background. Later when schedule alarm time has gone user put app to foreground by tapping app from list under App Switcher button. And for some reason app first display alert "Time gone" and latter (I think) do function <code>OnAppearing()</code>. Finally in main page I have a list of Notes with updated records, but why app displays this alert first?</p>
  
  <p>But this problem doesn't appear in three other cases.<br>
  User kill app in App Switcher list and open again tapping icon in application list.<br>
  User exit from app tapped Back Button.<br>
  User resume app by tapping notification.  </p>
  
  <p>So why if user resume app from App Switcher list, this alert is displayed but in other cases not?</p>
  
  <p>I hope my description is clear.
  Please explain to me why it happens and how to fix it.</p>