刚接触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:
----程序将要终止
以上就是程序在实际用户操作下可能发生的几种情况,开发者可自行在对应的方法下执行自己的代码。