Search code examples
notificationsxamarin.iosforeground

Monotouch: Controller brought to foreground - Notification?


MonoTouch

When an app is brought back to the foreground, I need the ViewController that becomes active to know about this.

Is there an event or override I can use to determine that the view was brought to the foreground.

I did find "WillEnterForegroundNotification" but it's a String, so am not sure how it's used.


Solution

  • I found this:

    Put this in the CTOR of the ViewController:

    NSNotificationCenter.DefaultCenter.AddObserver (UIApplication.WillEnterForegroundNotification, 
        EnterForeground); 
    

    Create then this method to handle the event in the view controller.

    void EnterForeground (NSNotification notification)
    {
        Console.WriteLine("EnterForeground: " + notification.Name); 
    }
    

    Note: When your app is brought to the foreground, the UIApplicationDelegate will get this raised first, a good place to clear things like login details and security related checks.

    public override void WillEnterForeground (UIApplication application)