Reload application data when the app comes into foreground?

前端 未结 4 476
轮回少年
轮回少年 2021-01-30 15:31

I\'m new to iPhone development. I\'m building an app that loads data from a local sqlite3 DB in

-   (BOOL)application:(UIApplication *)application didFinishLaunch         


        
4条回答
  •  遥遥无期
    2021-01-30 16:11

    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.

    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.

    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.

    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.


    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;

提交回复
热议问题