iOS 应用生命周期

1.程序首次启动(从磁盘加载到内存运行)

执行以下回调

a.application:didFinishLaunchingWithOptions(只执行一次 全新启动时 仅从程序变为进程)

完成了程序运行,等待接受事件,之后可以在此绘制界面,比如执行控制器中的

viewDidLoad完成界面显示

b.applicationDidBecomeActive(前台显示 激活状态)


2.按下HOME或电话 或 短信

a.applicationWillResignActive(已离开前台 非激活状态)

b.applicationDidEnterBackground(后台)

c.进入后台后不久(默认不能超过5秒,超过5秒会被系统清除进程进入未运行状态)会处于挂起状态


3.再次从图标启动

a.applicationWillEnterForeground(从后台状态 进入前台非激活状态)

b.applicationDidBecomeActive(进入激活状态 界面可以接受事件 看的见界面了)


注意:applicationWillTerminate 正常杀死进程不会调用,不要通过applicationWillTerminate

来保存应用程序的状态,而应该在applicationDidEnterBackground

非激活有2个回调 applicationWillResignActive(从激活进入后台的中间状态)和applicationWillEnterForeground(从后台进入激活的中间状态)


激活->非激活 applicationWillResignActive 或 UIApplicationWillResignActiveNotification来“暂停”应用的显示

非激活->后台 applicationDidEnterBackground或UIApplicationDidEnterBackgroundNotification

 释放应用处于后台时不需要的任何资源,可以降低被系统清除的风险。还应借此机会保存任何必要的应用数据,这些数据将有助于用户在下一次重新启动时找到上次离开的进度

后台->非激活 applicationWillEnterForeground或UIApplicationWillEnterForeground

 这里可恢复从非激活切换到后台状态时所执行的任何操作。例如 可以在这里重新建立网络连接

非激活->激活 applicationDidBecomeActive 或 UIApplicationDidBecomeActive恢复从激活到非激活所执行的所有操作


Not running  未运行  程序没启动

Inactive          未激活        程序在前台运行,不过没有接收到事件。在没有事件处理情况下程序通常停留在这个状态

Active             激活           程序在前台运行而且接收到了事件。这也是前台的一个正常的模式

Backgroud     后台           程序在后台而且能执行代码,大多数程序进入这个状态后会在在这个状态上停留一会。时间到之后会进入挂起状态(Suspended)。有的程序经过特殊的请求后可以长期处于Backgroud状态

Suspended    挂起           程序在后台不能执行代码。系统会自动把程序变成这个状态而且不会发出通知。当挂起时,程序还是停留在内存中的,当系统内存低时,系统就把挂起的程序清除掉,为前台程序提供更多的内存。

下图是程序状态变化图:



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

    // Override point for customization after application launch.

    NSLog(@"%@",NSStringFromSelector(_cmd));

    return YES;

}



- (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.

        NSLog(@"%@",NSStringFromSelector(_cmd));

}



- (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.

        NSLog(@"%@",NSStringFromSelector(_cmd));

}



- (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.

        NSLog(@"%@",NSStringFromSelector(_cmd));

}



- (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.

        NSLog(@"%@",NSStringFromSelector(_cmd));

}



- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

        NSLog(@"%@",NSStringFromSelector(_cmd));

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值