开发者

How to get notified with MonoTouch about my application being closed/sent to background?

Although I think this is a fairly trivial question, I could not find any answers out there.

My question is:

Is there a way to get a notification in a MonoTouch iPhone application when my application is being closed or sent to background (by a user clicking the home button)?

I thought the 开发者_运维问答WillTerminate override was good for this, but in the debugger, it is never called.


There are two ways to get notified when an app goes to the background:

a. Override the appropriate method in your AppDelegate:

public override void DidEnterBackground(UIApplication application)
{
    // App entered background, do some light stuff here, 
    // there is not much time before getting suspended
}

b. Add notification observers through the NSNotificationCenter class:

 NSObject observer = NSNotificationCenter.DefaultCenter.AddObserver(
    UIApplication.DidEnterBackgroundNotification, 
    delegate(NSNotification ntf) {

            // Same as above
    });

You can use the NSObject object returned from the AddObserver method to remove the observer when you no longer need it:

NSNotificationCenter.DefaultCenter.RemoveObserver(observer);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜