Page lifecycle events in xamarin.forms
Asked Answered
S

4

36

I just developed my first xamarin.forms app. I am excited about xamarin.forms, but I miss several events.

Are there any page-lifecycle-events in a xamarin.forms ContentPage?

I know of these two:

protected override void OnAppearing()
{
}

protected override void OnDisappearing()
{
}

But the OnAppearing() event only fires once. On Android, when I push the start button and go back to my app, this event does not fire again.

Is there a workaround for this (like OnNavigatedTo in WindowsPhone-pages)?

Slackjawed answered 25/3, 2015 at 9:52 Comment(0)
S
42

So the OnAppearing event is fired when your page is appearing. I.e. you have navigated to that page or back to that page from another in the Stack. There is currently no Page Lifecycle events as you can see from the API documentation

I think what you are talking about is if you put your application to sleep and navigate back into it the OnAppearing event is not fired, This is because your page hasn't appeared because it was already there, the application was just asleep.

What you are looking for is the App Lifecycle which includes methods such as:

protected override void OnStart()
{
    Debug.WriteLine ("OnStart");
}
protected override void OnSleep()
{
    Debug.WriteLine ("OnSleep");
}
protected override void OnResume()
{
    Debug.WriteLine ("OnResume");
}

You can then use the OnResume event to do what you are looking for.

That Xamarin Document includes the changes that must be made to old Xamarin projects to access these events. e.g. your App class in your shared library must inherit from Xamarin.Forms.Application and changes must also be made to the AppDelegate and MainActivity.

Sung answered 25/3, 2015 at 10:1 Comment(2)
I thought there are some other events I can use.. But now I can solve the problem. Thanks for the fast answer.Slackjawed
When is OnSleep called?Proclamation
P
10

You could also use Xamarin's MessagingCenter to perform arbitrary coordination of events: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/messaging-center/

I've been frustrated by the same thing: not having consistent view life cycle events in Xamarin.Forms. But you can get around some of those constraints by using the MessagingCenter, which is just a simple pub/sub messaging facility.

Praetor answered 26/3, 2015 at 15:51 Comment(3)
I've used the MessagingCenter bevore but I never got the idea to use it for this purpose. Good workaround, thank you.Slackjawed
I also met an inconsistent event cycles of Xamarin Android and UWP. I had a First tab page which uses parent page's CurrentPage to trigger the Second tab page's Appearing event. On Android, First tab can always triggers the Appearing event, however, on UWP, not always. The inconsistent problem is solved by MessagingCenter. Thank you!Commander
Thanks, this answer helped me with the problem of the missing OnAppearing() method invocation when a page shows up. I don’t know why Xamarin behaves like that, but for now, I can manage the problem with the MessaggingCenter.Auliffe
R
1

To build upon first answer https://mcmap.net/q/421332/-page-lifecycle-events-in-xamarin-forms,

Set flag on onappearing and ondisappearing, and based on it use onresume. i.e.

Contentpage.xaml.cs

protected override void OnAppearing()
{
    base.OnAppearing();
            
    App.DroidLocationPermissionAsker.AskLocationPermission();
    App.Locationpagepersist = true;
}

protected override void OnDisappearing()
{
    base.OnDisappearing();
    App.Locationpagepersist = false;
}

App.xaml.cs:

public static bool Locationpagepersist { get; internal set; }
protected override void OnResume()
{
    if (Locationpagepersist)
    {             
        DroidLocationPermissionAsker.AskLocationPermission();
    }    
}
Rom answered 13/7, 2022 at 11:53 Comment(0)
O
0

I use below code

protected override void OnAppearing()
{
    base.OnAppearing();
    // do your works
}
Oneway answered 22/5, 2023 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.