在AppDelegate中有几个关于生命周期的方法,今天在这里记录一下:
首先,和程序的生命周期方法对应的有几种程序状态:
(1)not running:app还没运行
(2)inactive:app运行在前台但没有接收事件
(3)active:app运行在foreground并且正在接收事件
(4)background:运行在后台且正在执行代码
(5)suspended:运行在background但没有执行代码
程序的初始化
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
程序将要关闭active状态
- (void)applicationWillResignActive:(UIApplication *)application;
程序将要已经进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application;
程序将要进入前台
- (void)applicationWillEnterForeground:(UIApplication *)application;
程序已经变成活跃
- (void)applicationDidBecomeActive:(UIApplication *)application;
程序将要结束
- (void)applicationWillTerminate:(UIApplication *)application;
下面说说APP的几种操作和将会用到的方法:
(1)启动APP:状态not running -> inactive -> active,调用方法didFinishLaunchingWithOptions -> applicationDidBecomeActive
(2)前台进入后台(锁屏,进入到主页面):active -> inactive -> background, 调用方法applicationWillResignActive -> applicationDidEnterBackground
(3)后台进入前台(解除锁屏,打开已经启动的APP):background -> foreground -> active,调用方法applicationWillEnterForeground -> applicationDidBecomeActive
(4)中断事件(如通知栏,电话,闹铃等):active -> inactive -> active,调用方法applicationWillResignActive -> applicationDidBecomeActive
(5)直接切换到其他APP:applicationDidEnterBackground
(6)删除:applicationWillTerminate,注意这里只有当页面在前台进行销毁,或者是在系统自动回收内存而销毁APP的时候调用,在后台的时候进行销毁不会调用