iOS开发:应用生命周期
- iOS应用通过委托对象AppDelegate类在应用周期的不同阶段会回调不同的方法,应用周期分为以下五种状态:
Not Running(非运行状态)。应用没有运行或被系统终止。
Inactive(前台非活动状态)。应用正在进入前台状态,但是还不能接受事件处理。
Active(前台活动状态)。应用进入前台状态,能接受事件处理。
Background(后台状态)。应用进入后台后,依然能够执行代码。如果有可执行的代码,就会执行代码,
如果没有可执行的代码或者将可执行的代码执行完毕,应用会马上进入挂起状态。
Suspended(挂起状态)。处于挂起的应用进入一种“冷冻”状态,不能执行代码。如果系统内存不够,
应用会被终止。 状态跃迁过程中应用回调的方法和本地通知
- 应用状态改变的回调方法和本地通知
方法 | 本地通知 | 说明 |
application:didFinishLaun- chingWithOptions: |
UIApplicationDidFinishLaunching- Notification |
应用启动并进行初始化时会调用该方法并发 出通知。这个阶段会实例化根视图控制器 |
applicationDidBecomeActive: |
UIApplicationDidBecomeActive- Notification |
应用进入前台并处于活动状态时调用该方法 并发出通知。这个阶段可以恢复UI的状态(例 如游戏状态等) |
applicationWillResignActive: |
UIApplicationWillResignActive- Notification |
应用从活动状态进入到非活动状态时调用该 方法并发出通知。这个阶段可以保存UI的状 态(例如游戏状态等) |
applicationDidEnterBackground: |
UIApplicationDidEnterBackground- Notification |
应用进入后台时调用该方法并发出通知。这 个阶段可以保存用户数据,释放一些资源(例 如释放数据库资源等) |
applicationWillEnterForeground: |
UIApplicationWillEnterForeground- Notification |
应用进入到前台,但是还没有处于活动状态 时调用该方法并发出通知。这个阶段可以恢 复用户数据 |
applicationWillTerminate: | UIApplicationWillTerminate- Notification | 应用被终止时调用该方法并发出通知,但内 存清除时除外。这个阶段释放一些资源,也 可以保存用户数据 |
- 具体代码可查看AppDelegate
import UIKit @UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
} func applicationWillResignActive(application: UIApplication) {
// 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.
} func applicationDidEnterBackground(application: UIApplication) {
// 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.
} func applicationWillEnterForeground(application: UIApplication) {
// 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.
} func applicationDidBecomeActive(application: UIApplication) {
// 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.
} func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
} }
iOS开发:应用生命周期的更多相关文章
- 【iOS开发】iOS对UIViewController生命周期和属性方法的解析
iOS对UIViewController生命周期和属性方法的解析 一.引言 作为MVC设计模式中的C,Controller一直扮演着项目开发中最重要的角色,它是视图和数据的桥梁,通过它的管理,将数据有 ...
- (转)iOS应用程序生命周期(前后台切换,应用的各种状态)详解
原文:http://blog.csdn.net/totogo2010/article/details/8048652 iOS应用程序生命周期(前后台切换,应用的各种状态)详解 分类: ...
- iOS对UIViewController生命周期和属性方法的解析
目录[-] iOS对UIViewController生命周期和属性方法的解析 一.引言 二.UIViewController的生命周期 三.从storyBoard加载UIViewController实 ...
- 转:iOS应用程序生命周期(前后台切换,应用的各种状态)详解
iOS应用程序生命周期(前后台切换,应用的各种状态)详解 分类: iOS开发进阶2012-10-08 15:35 42691人阅读 评论(30) 收藏 举报 iosapplication任务anima ...
- android开发之生命周期
android开发之生命周期 一:Activity的生命周期: 这几天了了解了安卓Activity的生命周期,对于生命周期有了大概的理解: 一个Activity的生命周期也就是Activity从生成到 ...
- Android开发——Activity生命周期
Android开发--Activity生命周期 Activity作为四大组件之首,也是使用最频繁的一种组件.本文将主要讲解Activity生命周期,包括正常情况下的Activity生命周期和异常情况下 ...
- 浅尝Spring注解开发_Bean生命周期及执行过程
Spring注解开发 浅尝Spring注解开发,基于Spring 4.3.12 包含Bean生命周期.自定义初始化方法.Debug BeanPostProcessor执行过程及在Spring底层中的应 ...
- iOS - ViewController的生命周期
iOS SDK中提供很多原生的ViewController,大大提高了我们的开发效率:那么下面我们就根据开发中我们常用的ViewController谈一谈它的生命周期: (一)按照结构和用法可以对iO ...
- iOS控制器的生命周期分析和使用
转自http://blog.csdn.net/qijianli/article/details/7826979 iOS的SDK中提供很多原生ViewController,大大提高了我们的开发效率,下面 ...
- [转]iOS应用程序生命周期(前后台切换,应用的各种状态)详解
转载地址:http://blog.csdn.net/totogo2010/article/details/8048652 iOS的应用程序的生命周期,还有程序是运行在前台还是后台,应用程序各个状态的变 ...
随机推荐
- OC的@property 和 @synthesize id
学习java的JDBC,成员变量的setter和getter,eclipse都能帮我们自动生成:当然xcode这款编译器也很强大,也能自动生成: 1:@property @property是写在类的声 ...
- Careercup - Facebook面试题 - 5188884744896512
2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...
- 【HTTP】Fiddler(一) - Fiddler简介
1.为什么是Fiddler? 抓包工具有很多,小到最常用的web调试工具firebug,达到通用的强大的抓包工具wireshark.为什么使用fiddler?原因如下: a.Firebug虽然可以抓包 ...
- PE文件结构详解(五)延迟导入表
PE文件结构详解(四)PE导入表讲 了一般的PE导入表,这次我们来看一下另外一种导入表:延迟导入(Delay Import).看名字就知道,这种导入机制导入其他DLL的时机比较“迟”,为什么要迟呢?因 ...
- Spring事务配置的五种方式(转)
前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ...
- 牛顿迭代法实现平方根函数sqrt
转自利用牛顿迭代法自己写平方根函数sqrt 给定一个正数a,不用库函数求其平方根. 设其平方根为x,则有x2=a,即x2-a=0.设函数f(x)= x2-a,则可得图示红色的函数曲线.在曲线上任取一点 ...
- HDU 1546 Idiomatic Phrases Game(最短路,Dijsktra,理解题意很重要)
题目 1.注意因为要判断能不能到达,所以要在模版里面判断k有没有更新. 2.看懂题目意思和案例的解法很重要. #define _CRT_SECURE_NO_WARNINGS //题目大意:现要进行单词 ...
- Intent (一)
1,简介 Intent 是一种消息传递机制,可以理解为一种对消息的封装,执行某操作的抽象描述,可用于应用程序内部及应用程序之间 其组成包括: 要执行的动作(action) 如VIEW_ACTION(查 ...
- poj 1026 Cipher
置换群就可以搞定!!! 注意下格式就好了…… #include<iostream> #include<stdio.h> #include<algorithm> #i ...
- IOS时间格式转换
在开发iOS程序时,有时候需要将时间格式调整成自己希望的格式,这个时候我们可以用NSDateFormatter类来处理. 例如:如何将格式为“12-May-14 05.08.02.000000 PM” ...