Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

iphone - Use of background/foreground methods in AppDelegate

I'm planning to implement multi-task in my app. I can see many methods here to do that in the AppDelegate like applicationWillResignActive, applicationDidEnterBackground, applicationWillEnterForeground, ...

But.... I don't see the way they should be used, nor why they are not in the ViewControllers... Nor what they are here for.

I mean : when the app enter in background, i don't know on which view my user is. And back, when the app comes into foreground, how would I know what to do and what I may call, to update the view for example ?

I would have understood if those methods where in each view controller, but here, I don't see what they can be used for in a concrete way...

Can you help me to understand the way to implement things into those methods ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Each object receive a UIApplicationDidEnterBackgroundNotification notification when the app goes in background. So to run some code when the app goes in background, you just have to listen to that notification where you want :

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(appHasGoneInBackground:)
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:nil];

Don't forget to release the listener when you don't need to listen to it anymore :

[[NSNotificationCenter defaultCenter] removeObserver:self];

And best of the best, you can play the same way with the following notifications :

  • UIApplicationDidEnterBackgroundNotification
  • UIApplicationWillEnterForegroundNotification
  • UIApplicationWillResignActiveNotification
  • UIApplicationDidBecomeActiveNotification

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...