Search code examples
c#facebookxamarin.iosxamarin.formsvisual-studio-mac

What is the correct way to override OnActivated in Xamarin Forms? Am getting unexpected results


The Facebook SDK directs me to use the OnActivated event to log certain events. (or to selectively not log them.)

It tells me to override the iOS OnActivated event. (see also Xamarin docs.)

When I type in "override OnActivated" the following code is generated

    [Export("applicationDidBecomeActive:")]
    public void OnActivated(UIApplication application)
    {
        throw new System.NotImplementedException();
    }

this is in contrast to every other override, there is no export

    public override void WillEnterForeground(UIApplication uiApplication)
    {
    }

Does this mean that Xamarin has a bug, or this is a different implementation?


Solution

  • Below is the correct way to override OnActivated for Xamarin.iOS.

    The Export attribute will appear when you try to override OnActivated when your AppDelegate already has an existing public override void OnActivated method.

    I have tested the below code in my Xamarin.iOS app and it triggers as expected when the app is activated.

    Code

    public class AppDelegate : FormsApplicationDelegate
    {
        ...
    
        public override void OnActivated(UIApplication uiApplication)
        {
            base.OnActivated(uiApplication);
    
            //Do Stuff
        }
    
        ....
    }