应用程序的状态

IOS的应用程序一共有5种状态。

  • Not running(未运行):程序未启动
  • Inactive(未激活):其他两个状态切换时出现的短暂状态。唯一在此状态停留时间比较长的情况是:当用户锁屏时?或者系统提示用户去响应Alert窗口(如来电、信息)时
  • Active(激活):在屏幕上显示的正常运行状态,该状态下可以接收用户输入并更新显示
  • Backgroud(后台):程序在后台且能执行代码。用户按下Home键不久后进入此状态(先进入了Inactive状态,再进入Background状态),然后会迅速进入挂起状态(Suspended)。有的程序经过特殊的请求后可以长期处于Backgroud状态
  • Suspended(挂起):程序在后台不能执行代码。普通程序在进入Background状态不久后就会进入此状态。当挂起时,程序还是停留在内存中的,当系统内存低时,系统就把挂起的程序清除掉,为前台程序提供更多的内存

这个图非常重要,每一个箭头都需要细细研读。

关于Active和Inactive的切换:

应用程序在前台时有2种状态:Active和Inactive。大多数情况下,Inactive状态只是其他两个状态切换时出现的短暂状态(不是任意两个状态之间的切换都会进入Inactive,见图),如打开应用,它会从Not Running先进入Inactive再进入Active;如前后台应用切换时,Inactive会在Active和Background之间短暂出现。

但是也有其他情况,Active和Inactive可以在前台运行时进行切换,比如系统弹出Alert,此时应用会从Active切换到Inactive,直到用户确认再返回Actvie;再如用户拉下通知页,也会发生Active和Inactive的切换;还有来电但拒接、双击Home键但返回原应用等都不进入Background,而只是在Active和Inactive切换。

如图,是否可以这样说,要想进入Active必须先进入Inactive。

入口函数

int main(int argc, char * argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([XYZAppDelegate class]));
}
}
// If nil is specified for principalClassName, the value for NSPrincipalClass from the Info.plist is used. If there is no

// NSPrincipalClass key specified, the UIApplication class is used. The delegate class will be instantiated using init.

UIKIT_EXTERN int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);

argc和argv是为了与C语言保持一致,在这没用到,不详述。

后面两个参数为principalClassName(主要类名)和delegateClassName(委托类名)。

如果principalClassName是nil,那么它的值将从Info.plist中获取,如果Info.plist中没有,则默认为UIApplication。principalClass这个类除了管理整个程序的生命周期之外什么都不做,它只赋值监听事件然后交给delegateClass去做。

delegateClass将在工程新建时实例化一个对象

NSStringFromClass([XYZAppDelegate class])

相当于@"XYZAppDelegate"。

AppDelegate类六个方法

注意代码中的官方注释。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
NSLog(@"didFinishLaunchingWithOptions");
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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.
NSLog(@"WillResignActive");
} - (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(@"DidEnterBackground");
} - (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
NSLog(@"WillEnterForeground");
} - (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(@"DidBecomeActive");
} - (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
NSLog(@"WillTerminate");
}

启动程序

2014-07-28 15:22:39.883 LifeCycle[3024:a0b] didFinishLaunchingWithOptions

2014-07-28 15:22:39.887 LifeCycle[3024:a0b] DidBecomeActive

按下Home键

2014-07-28 15:22:43.130 LifeCycle[3024:a0b] WillResignActive

2014-07-28 15:22:43.131 LifeCycle[3024:a0b] DidEnterBackground

重新点击程序

2014-07-28 15:22:44.380 LifeCycle[3024:a0b] WillEnterForeground

2014-07-28 15:22:44.380 LifeCycle[3024:a0b] DidBecomeActive

注意:

  • 启动程序并没有调用WillEnterForeground这个方法。
  • 并不是所有状态切换都有相应的方法来通知,比如从Background到Suspended。所以当你按下Home键的时候,我们只知道调用了WillResignActive和DidEnterBackground方法,但其实应用程序会迅速从Background进入Suspended。

1.application:didFinishLaunchingWithOptions:

程序首次已经完成启动时执行,若直接启动,launchOptions中没有数据;否则,launchOptions将包含对应方式的内容(比如从微信中启动节奏大师--)。

2.applicationWillResignActive(将进入后台)

程序将要失去Active状态时调用,比如按下Home键或有电话信息进来。对应applicationWillEnterForeground(将进入前台),这个方法用来

  • 暂停正在执行的任务;
  • 禁止计时器;
  • 减少OpenGL ES帧率;
  • 若为游戏应暂停游戏;

总结为一个字:

3.applicationDidEnterBackground(已经进入后台)

程序已经进入后台时调用,对应applicationDidBecomeActive(已经变成前台),这个方法用来

  • 释放共享资源;
  • 保存用户数据(写到硬盘);
  • 作废计时器;
  • 保存足够的程序状态以便下次恢复;

总结为4个字:释放、保存

4.applicationWillEnterForeground(将进入前台)

程序即将进去前台时调用,对应applicationWillResignActive(将进入后台)。这个方法用来撤销applicationWillResignActive中做的改变。

5.applicationDidBecomeActive(已经进入前台)

程序已经变为Active(前台)时调用。对应applicationDidEnterBackground(已经进入后台)。若程序之前在后台,最后在此方法内刷新用户界面。

6.applicationWillTerminate

程序即将退出时调用。记得保存数据,如applicationDidEnterBackground方法一样。

如果你的类是AppDelegate类(声明遵循UIApplicationDelegate协议),那么可以实现上面的6个方法,当App状态改变的时候相应的方法会被调用;如果你的类不是AppDelegate类,那么该类如何知道App的各种状态变化,以及如何使用这些函数呢?答案是使用NotificationCenter来通知。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive) name:UIApplicationWillResignActiveNotification object:[UIApplication sharedApplication]];

然后实现applicationWillResignActive就行了

- (void)applicationWillResignActive        //自定义的函数
{
NSLog(@"%@", NSStringFromSelector(_cmd));
}

参考文章

iOS学习笔记(四)——iOS应用程序生命周期

iOS应用程序生命周期(前后台切换,应用的各种状态)详解

iOS应用程序的状态及其切换(生命周期)

iOS Application Life Cycle 应用程序生命周期的更多相关文章

  1. 图解ios程序生命周期

    图解ios程序生命周期 应用程序启动后状态有Active.Inactive.Background.Suspended.Not running这5种状态,几种状态的转换见下图: 在AppDelegate ...

  2. (转)iOS应用程序生命周期(前后台切换,应用的各种状态)详解

    原文:http://blog.csdn.net/totogo2010/article/details/8048652 iOS应用程序生命周期(前后台切换,应用的各种状态)详解         分类:  ...

  3. IOS应用程序生命周期&启动周期函数

    —程序的生命周期         a.程序的生命周期是指应用程序启动到应用程序结束整个阶段的全过程         b.每一个IOS应用程序都包含一个UIApplication对象,IOS系统通过该U ...

  4. 转:iOS应用程序生命周期(前后台切换,应用的各种状态)详解

    iOS应用程序生命周期(前后台切换,应用的各种状态)详解 分类: iOS开发进阶2012-10-08 15:35 42691人阅读 评论(30) 收藏 举报 iosapplication任务anima ...

  5. iOS学习笔记(四)——iOS应用程序生命周期

    开发应用程序都要了解其生命周期,开始接触android时也是从应用程序生命周期开始的,android的应用程序生命周期更多是其组件的生命周期,例如Activity.Service.今天我们接触一下iO ...

  6. 【iOS开发-72】设置状态栏的两种方式、程序生命周期以及更好地理解几大类(对象)之间的关系

    (1)设置状态栏的2种方式 --第一种方式就是我们在控制器中设置,系统默认就是交给视图控制器去管理的,这样不同视图控制器能够自己定义不同的状态栏例如以下: -(BOOL)prefersStatusBa ...

  7. IOS的工程目录结构和生命周期

    IOS的工程目录结构和生命周期 ·simple table文件夹:工程相关源代码和配置文件 BIDAppDelegate :    委托的声明和实现 BIDViewController:    视图控 ...

  8. asp.net应用程序生命周期和asp.net网页的生命周期

    一.asp.net应用程序生命周期 asp.net应用程序生命周期以浏览器向web服务器(比如IIS服务器)发送请求为起点,先后经历web服务器下的ISAPI(Internet Server Appl ...

  9. asp.net应用程序生命周期

    asp.net应用程序生命周期(流程文字描述版) 请求——>IIS——>ISAPI映射——>交给asp.net(即为IIS的扩展)——>通知Application类创建一个应用 ...

随机推荐

  1. C语言 百炼成钢2

    //题目4:输入某年某月某日,判断这一天是这一年的第几天? #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<st ...

  2. C语言 文件操作7--文件错误处理

    //文件错误处理 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include&l ...

  3. GPS坐标换算为百度坐标

    最近在做一个关于手机定位的小应用,需求是这样的,用户通过手机(Wp8)进行二维码扫描操作并且记录用户的当前位置,在PC上可以查看用户所在地图的位置,做法就是在用户扫描条码时,通过手机GPS获取当前在地 ...

  4. win7(X64)+VS2013+OpenCV3.1环境配置

    &1 源文件 VS2013: 链接:http://pan.baidu.com/s/1o8EKQq2 密码:open OpenCV3.1:  链接:http://pan.baidu.com/s/ ...

  5. 北京联想招聘-IOS高级 加入qq 群:220486180 或者直接在此 留言咨询

    ios 高级开发 Job ID #: 47980 Position Title: 高级iOS development engineer Location: CHN-Beijing Functional ...

  6. Object C学习笔记24-关键字总结

    学习Object C也有段时间了,学习的过程中涉及到了很多Object C中的关键字,本文总结一下所涉及到的关键字以及基本语法. 1.  #import #import <> 从syste ...

  7. 编写高质量代码改善C#程序的157个建议[泛型集合、选择集合、集合的安全]

    前言   软件开发过程中,不可避免会用到集合,C#中的集合表现为数组和若干集合类.不管是数组还是集合类,它们都有各自的优缺点.如何使用好集合是我们在开发过程中必须掌握的技巧.不要小看这些技巧,一旦在开 ...

  8. AJAX练习(一):制作可以自动校验的表单(从原理上分析ajax的作用)

    继上文(AJAX(一)AJAX的简介和基础)作为联系. 传统网页在注册时检测用户名是否被占用,传统的校验显然缓慢笨拙. 当ajax出现后,这种体验有了很大的改观,因为在用户填写表单时,签名的表单项已经 ...

  9. iOS - 视频循环播放

    录制完视频后,我们想在录制视频的预览层上无限循环播放我们的小视频,是不是很炫酷,这时候我们就有三中选择了:1.MPMoviePlayerController2.AVPlayer3.AVAssetRea ...

  10. 安装xampp二三事

    1.chrome 找不到页面时会自动跳转到hao123 安装完chrome后,想测试下localhost,结果找不到页面,当然正常的显示是“该页面无法显示”才对,可恨啊,总是直接转到hao123页面上 ...