ios 当应用程序进入前台时重新加载应用程序数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3943205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reload application data when the app comes into foreground?
提问by Cris
i'm new to iPhone dev ... i'm building an app that loads data from a local sqlite3 db in
我是 iPhone 开发新手……我正在构建一个应用程序,它从本地 sqlite3 db 中加载数据
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
When i tap iPhone button and put it in background and then i recall it i see (as normal) the app in the same way i leaved it. What i would like to do is, when it comes in foregroud, to reload data as if it was called from scratch.
当我点击 iPhone 按钮并将其置于背景中,然后我回想起它时,我看到(正常)该应用程序的方式与我离开它时的方式相同。我想做的是,当它进入前台时,重新加载数据,就像从头开始调用一样。
Which is the correct way to do it?
哪个是正确的方法?
Thanks in advance c.
提前致谢 c.
回答by sbs
So in the App Delegate class
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
will only be called at the first time you enter the app.Then it will call - (void)applicationDidBecomeActive:(UIApplication *)application
.
所以在 App Delegate 类中- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
只会在你第一次进入应用程序时
被调用- (void)applicationDidBecomeActive:(UIApplication *)application
。然后它会调用.
If your iphone iOS is 4.0 or later, when a user click on the home button, it will first invoke - (void)applicationWillResignActive:(UIApplication *)application
, then - (void)applicationDidEnterBackground:(UIApplication *)application
.
如果你的 iphone iOS 是 4.0 或更高版本,当用户点击主页按钮时,它会先调用- (void)applicationWillResignActive:(UIApplication *)application
,然后- (void)applicationDidEnterBackground:(UIApplication *)application
.
Then the app will run in background till the user kill it. When the user enter the app again, it will first invoke - (void)applicationWillEnterForeground:(UIApplication *)application
, then - (void)applicationDidBecomeActive:(UIApplication *)application
.
然后应用程序将在后台运行,直到用户杀死它。当用户再次进入应用程序时,它会先调用- (void)applicationWillEnterForeground:(UIApplication *)application
,然后调用- (void)applicationDidBecomeActive:(UIApplication *)application
。
So related to your question, you should call either applicationWillEnterForeground:
or applicationDidBecomeActive:
to reload your data. Though in the xcode's comment of these methods, Apple suggests use applicationDidBecomeActive:
to restart paused tasks and/or update user interface; while in the applicationWillEnterForeground:
, you can undo the changes you've made in entering the background.
与您的问题相关,您应该致电applicationWillEnterForeground:
或applicationDidBecomeActive:
重新加载您的数据。尽管在 xcode 对这些方法的评论中,Apple 建议使用applicationDidBecomeActive:
来重新启动暂停的任务和/或更新用户界面;在 中applicationWillEnterForeground:
,您可以撤消您在进入后台时所做的更改。
So to make it easier to see, i put a number tag to each method. Here's when the method to be called.
所以为了更容易看到,我给每个方法都加了一个数字标签。这是要调用的方法的时间。
0 application:(UIApplication *)application didFinishLaunchingWithOptions:
1 applicationDidBecomeActive:
2 applicationWillResignActive:
3 applicationDidEnterBackground:
4 applicationWillEnterForeground:
First enter the app: call 0, then 1;
Hit home button: call 2, then 3;
Double hit home button(multitasking): call 2;
if user choose another app or hit home button again: call 3;
if user double hit home button again: call 1;
Enter app again: call 4, then 1;
先进入app:先拨0,再拨1;
按home键:先拨2,再拨3;
双击home键(多任务):call 2;
如果用户选择另一个应用程序或再次点击主页按钮:调用 3;
如果用户再次双击主页按钮:调用 1;
再次进入app:拨打4,然后1;
回答by SegFault
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
Reload the data in the above function of the app delegate to refresh the data when ever the app comes to foreground.
在应用程序委托的上述函数中重新加载数据以在应用程序进入前台时刷新数据。
回答by Nik Reiman
You can exit()
your application when iOS notifies it that it should run in the background like so:
exit()
当 iOS 通知它应该在后台运行时,您可以像这样运行您的应用程序:
- (void)applicationDidEnterBackground:(UIApplication *)application {
exit(0);
}
回答by Beno?t
I think that is the delegate of UIApplication:
我认为这是 UIApplication 的委托:
- (void)applicationWillEnterForeground:(UIApplication *)application
In iOS 4.0 and later, this method is called as part of the transition from the background to the active state. You can use this method to undo many of the changes you made to your application upon entering the background. The call to this method is invariably followed by a call to the applicationDidBecomeActive: method, which then moves the application from the inactive to the active state.
在 iOS 4.0 及更高版本中,此方法作为从后台到活动状态转换的一部分被调用。您可以使用此方法撤消在进入后台时对应用程序所做的许多更改。调用此方法后总是调用 applicationDidBecomeActive: 方法,然后将应用程序从非活动状态移动到活动状态。