Reload application data when application comes to the fore?

I am new to iPhone dev ... I am creating an application that loads data from local sqlite3 db into

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

When I press the iPhone button and place it in the background, and then I find it, I see (as usual) the application just like I left it. What I would like to do is when it enters the foregroud to reload the data, as if it had been called from scratch.

What is the right way to do this?

Thanks in advance with.

+6
ios multitasking
source share
4 answers
 - (void)applicationDidBecomeActive:(UIApplication *)application { } 

Reload the data in the above application delegate function to update the data when the application appears in the foreground.

+13
source share

So, the App Delegate class - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions will be called only at the first entrance to the application. Then it will call - (void)applicationDidBecomeActive:(UIApplication *)application .

If your iphone iOS is 4.0 or later, when the user clicks the home button, he first calls - (void)applicationWillResignActive:(UIApplication *)application , then - (void)applicationDidEnterBackground:(UIApplication *)application .

Then the application will start in the background until the user kills it. When the user starts the application again, he will first call - (void)applicationWillEnterForeground:(UIApplication *)application , then - (void)applicationDidBecomeActive:(UIApplication *)application .

In connection with your question, you should call either applicationWillEnterForeground: or applicationDidBecomeActive: to reload your data. Although Apple suggests using applicationDidBecomeActive: in the xcode comment of these methods applicationDidBecomeActive: to restart suspended tasks and / or update the user interface; and in applicationWillEnterForeground: you can undo the changes you made when entering the background.


So, to make browsing easier, I put a number tag in each method. Here when the method is called.

 0 application:(UIApplication *)application didFinishLaunchingWithOptions: 1 applicationDidBecomeActive: 2 applicationWillResignActive: 3 applicationDidEnterBackground: 4 applicationWillEnterForeground: 
  • First enter the application: call 0, then 1;

  • Press the home button: call 2, then 3;

  • Button for double pressing the house (multitasking): call 2;

    • if the user selects another application or presses the home button again: call 3;

    • if the user has pressed the home button twice again: call 1;

  • Enter the application again: call 4, then 1;

+62
source share

You can exit() your application when iOS tells it that it should run in the background like this:

 - (void)applicationDidEnterBackground:(UIApplication *)application { exit(0); } 
+3
source share

I think this is a UIApplication delegate:

 - (void)applicationWillEnterForeground:(UIApplication *)application 

In iOS 4.0 and later, this method is called part of the transition from the background to the active state. You can use this method to undo many of the changes you made when entering the background. A call to this method is invariably followed by a call to applicationDidBecomeActive: a method that then moves the application from an inactive to an active state.

+3
source share

All Articles