José
José

Reputation: 165

call onresumo() method within a contentpage XAMARIN FORMS

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

Upvotes: 4

Views: 2782

Answers (2)

Dbl
Dbl

Reputation: 5914

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

Upvotes: 8

hvaughan3
hvaughan3

Reputation: 11105

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

Upvotes: 7

Related Questions