Ian Vink
Ian Vink

Reputation: 68770

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.

Upvotes: 4

Views: 908

Answers (2)

David Clarke
David Clarke

Reputation: 13266

In my MonoTouch app I have an "add" button that once tapped is then disabled for the rest of the day. To check and enable the button when the app becomes active I monitor UIApplication.Notifications.ObserveDidBecomeActive in the constructor for the ViewController.

NSObject _didBecomeActiveNotification;
.
.
.
// and in constructor
_didBecomeActiveNotification = UIApplication.Notifications.ObserveDidBecomeActive((sender, args) => {
    SetRightBarButtonState();
});

Then I override the Dispose method in the ViewController via

protected override void Dispose (bool disposing) {
    if (disposing) {
        _didBecomeActiveNotification.Dispose();
    }
    base.Dispose (disposing);
}

Upvotes: 0

Ian Vink
Ian Vink

Reputation: 68770

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)

Upvotes: 8

Related Questions