IOS程序生命週期

        剛接觸IOS的學習,做點小筆記,總結下,加深下記憶。第一篇文章講述IOS程序的生命週期也就是類似於Android Activity的生命週期,但是在IOS中只有在AppDelegate.h文件中存在生命週期,而在單獨的Control中不存在生命週期的使用,也就是相當於Android的Application和Activity生命週期的結合體。廢話不多說下面進入正題。
        先解釋下AppDelegate的各個方法的意義。

一、application: didFinishLaunchingWithOptions:
        此方法在程序啓動以後之後執行,只有在第一次程序啓動之後纔會執行,相當於Android中的onCreate。

//只有在程序啓動第一次纔會調用該方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}

二、applicationDidBecomeActive:
        此方法表示程序已經獲得焦點,每次進入前臺最後調用的就是該方法,相當於Android中的onResume。

//表示程序獲得到焦點,每次程序前臺後最後執行該方法
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

三、applicationWillEnterForeground:
        該方法表示程序進入前臺,啓動程序或進入程序後觸發的方法,相當於Android的onStart。

//程序即將進入前臺
- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

四、applicationWillResignActive:
        該方法表示程序失去焦點,程序進入後臺第一個觸發的方法,相當於Android中的onPause。

//程序將要失去焦點
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

五、applicationDidEnterBackground:
        該程序表示程序即將進入後臺,進入後臺最後執行的方法,相當於Android的onStop。

//程序即將進入後臺
- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

六、applicationWillTerminate:
        該方法表示程序即將終止,相當於Android中的Application的onDestory的方法。

//程序即將終止的方法
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

七、applicationDidReceiveMemoryWarning
        該方法表示程序內存不足時,IOS系統發送警告執行的方法。

//程序接受到內存不足的通知執行的方法
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application{
}

八、實際用戶操作生命週期的調用
        上面簡要描述了AppDelegate中各個方法的作用,下面介紹用戶在實際操作軟件情況下各個函數的執行情況。

(1)第一次進入程序
1.application: didFinishLaunchingWithOptions:
    ----程序啓動
2.applicationDidBecomeActive:
    ----程序獲得焦點

(2)程序按下Home鍵(或者切換到其他程序)
1.applicationWillResignActive:
    ----程序將要失去焦點
2.applicationDidEnterBackground:
    ----程序進入後臺

(3)重新進入程序
1.applicationWillEnterForeground:
    ----程序進入前臺
2.applicationDidBecomeActive:
    ----程序獲得焦點

(4)下拉狀態欄
1.applicationWillResignActive:
    ----程序將要失去焦點
    ----注:程序只是失去了焦點,而不會步入後臺

(5)狀態欄收回
1.applicationDidBecomeActive:
    ----程序獲得焦點

(6)雙擊Home關閉程序
1.applicationWillResignActive:
    ----程序將要失去焦點
2.applicationDidEnterBackground:
    ----程序進入後臺
3.applicationWillTerminate:
    ----程序將要終止

        以上就是程序在實際用戶操作下可能發生的幾種情況,開發者可自行在對應的方法下執行自己的代碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章