Problem with app after resume.

DomKapDomKap USMember ✭✭

Hi.
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.
To the point.

Note model:

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; }
}

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".

XAML
<local:ButtonActiveSwitcher Toggled="Switch_Toggled" IsToggled="{Binding AlarmTimeActive}" Active="{Binding .}" />
function "Switch_Toggled"

    private void Switch_Toggled(object sender, ToggledEventArgs e)
            {    
                var switchBtn = sender as Switch;

                var item = ((ButtonActiveSwitcher)sender).Active;          
                if (item != null)
                {
                    if (item.AlarmTime < DateTime.Now)
                    {
                        if (_nooLoopTime.AddSeconds(2) < DateTime.Now) //Prevent double display alert
                        {
                            DisplayAlert("ALERT", "Time gone", "OK");
                            _nooLoopTime = DateTime.Now;
                        }                    
                        switchBtn.IsToggled = false;
                        return;
                    }                
                    DataBaseService.updateRecord(item);
                }           
            }

And this function works fine when user tapped switcher.
Next point.

In MainPage.cs in function onAppearing app fired function DataBaseService.checkNoteAlarmTimeActive();. In this function app check AlarmTime in notes. If AlarmTimeActive is active but schedule time has gone then change AlarmTimeActive to "false".

First app checks Notes in DB and update them, next function loadNotes() getting Notes from DB and populate list.

So before app gets Notes from DB first update records in DB.

MainPage.cs

[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();
    }

    /// <summary>
    /// Generate list of notes basic on current sorting option and active switcher
    /// </summary>     
    private async void loadNotes()
    {
        listNotes.ItemsSource = await _sorting.sortNotes(_sortOption, _activeSwitcherState);          
    }

And here is my problem.
For example: one Note has AlarmTimeActive "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 OnAppearing(). Finally in main page I have a list of Notes with updated records, but why app displays this alert first?

But this problem doesn't appear in three other cases.
User kill app in App Switcher list and open again tapping icon in application list.
User exit from app tapped Back Button.
User resume app by tapping notification.

So why if user resume app from App Switcher list, this alert is displayed but in other cases not?

I hope my description is clear.
Please explain to me why it happens and how to fix it.

Thank you.

Tagged:
Sign In or Register to comment.