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)?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Joehl
  • 3,671
  • 3
  • 25
  • 53

4 Answers4

41

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.

JKennedy
  • 18,150
  • 17
  • 114
  • 198
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.

NovaJoe
  • 4,595
  • 6
  • 29
  • 43
  • I've used the MessagingCenter bevore but I never got the idea to use it for this purpose. Good workaround, thank you. – Joehl Mar 27 '15 at 06:59
  • 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! – Meng-Yuan Huang Feb 24 '17 at 00:50
  • 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`. – Pine Code Dec 13 '20 at 23:29
1

To build upon first answer https://stackoverflow.com/a/29252552/1894630,

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();
    }    
}
ms2r
  • 170
  • 3
  • 17
0

I use below code

protected override void OnAppearing()
{
    base.OnAppearing();
    // do your works
}
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Hieu.Gi
  • 77
  • 6