4
votes

I am having the following problem in my statement in iOS. I need to activate a command at the moment the user returns to the application when it is in the background. On my ContentPage I can not find a method to identify the return. In Android, it works correctly, only in iOS, I can not find any function in the content that shows me that I am returning

 protected override void OnAppearing()
    {
        Debug.WriteLine("OnAppearing()");
        base.OnAppearing();
    }

    protected override void OnDisappearing()
    {
        Debug.WriteLine("OnDisappearing");
        base.OnDisappearing();
    }

    protected override void OnBindingContextChanged()
    {
        Debug.WriteLine("OnBindingContextChanged");
        base.OnBindingContextChanged();
    }

I tried the three options but no results

2
Is this what you're looking for maybe? forums.xamarin.com/discussion/63577/… - josemigallas
Onresume is properly called in my App.xaml.cs, to work with my application I would have to call that onresume inside a contentpage - José
Yes, sorry, didn't realised you were using XF. Silly suggestion maybe but have you tried calling the base method first? :P - josemigallas
without results, it continues to only cast onResume () in the App. and does not call any content - José

2 Answers

6
votes

As josemigallas said, you are looking for App.OnResume(). You can easily subscribe and unsubscribe to a MessagingCenter event which can be sent from your App.OnResume() method to trigger actions in each of your ContentPages that are subscribed (do not forget to unsubscribe from the event within ContentPage.OnDisappearing() to prevent memory leaks):

In your ContentPages:

protected override void OnAppearing() {
    Debug.WriteLine("OnAppearing()");
    base.OnAppearing();

    MessagingCenter.Subscribe<App>(this, "SpecialOnResumeKeyValue", app => {
            //Do something
    };
}

protected override void OnDisappearing() {
    Debug.WriteLine("OnDisappearing");
    base.OnDisappearing();

    MessagingCenter.Unsubscribe<App>(this, "SpecialOnResumeKeyValue"); //VERY IMPORTANT
}

Then in your App.xaml.cs:

protected override async void OnResume() {
    MessagingCenter.Send(this, "SpecialOnResumeKeyValue");
}
6
votes

I usually do it this way so i don't have to bother with subscriptions:

public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        MainPage = new MainPage();
    }

    protected override async void OnStart()
    {
        if (Application.Current.MainPage is IAppStateAware appStateAwarePage)
            await appStateAwarePage.OnStartApplicationAsync();
        if (Application.Current.MainPage.BindingContext is IAppStateAware appStateAwareVm)
            await appStateAwareVm.OnStartApplicationAsync();
        // Handle when your app starts
    }

    protected override async void OnSleep()
    {
        if (Application.Current.MainPage is IAppStateAware appStateAwarePage)
            await appStateAwarePage.OnSleepApplicationAsync();
        if (Application.Current.MainPage.BindingContext is IAppStateAware appStateAwareVm)
            await appStateAwareVm.OnSleepApplicationAsync();
        // Handle when your app sleeps
    }

    protected override async void OnResume()
    {
        if (Application.Current.MainPage is IAppStateAware appStateAwarePage)
            await appStateAwarePage.OnResumeApplicationAsync();
        if (Application.Current.MainPage.BindingContext is IAppStateAware appStateAwareVm)
            await appStateAwareVm.OnResumeApplicationAsync();
        // Handle when your app resumes
    }
}

public class YourContentPage : Page, IAppStateAware
{
    public Task OnResumeApplicationAsync()
    {
        throw new System.NotImplementedException();
    }

    public Task OnSleepApplicationAsync()
    {
        throw new System.NotImplementedException();
    }

    public Task OnStartApplicationAsync()
    {
        throw new System.NotImplementedException();
    }
}

public interface IAppStateAware
{
    Task OnResumeApplicationAsync();
    Task OnSleepApplicationAsync();
    Task OnStartApplicationAsync();
}