iOS开发之程序各种状态监听
<div>1、程序的五种状态<br>Not Running:未运行。<br>Inactive:前台非活动状态。处于前台,但是不能接受事件处理。<br>Active:前台活动状态。处于前台,能接受事件处理。<br>Background:后台状态。进入后台,如果又可执行代码,会执行代码,代码执行完毕,程序进行挂起。<br>Suspended:挂起状态。进入后台,不能执行代码,如果内存不足,程序会被杀死。<br><br></div><div>2、AppDelegate中的回调方法和通知<br>(1)回调方法:application:didFinishLaunchingWithOptions:<br> 本地通知:UIApplicationDidFinishLaunchingNotification<br> 触发时机:程序启动并进行初始化的时候后。<br> 适宜操作:这个阶段应该进行根视图的创建。<br>(2)回调方法:applicationDidBecomeActive:<br> 本地通知:UIApplicationDidBecomeActiveNotification<br> 触发时机:程序进入前台并处于活动状态时调用。<br> 适宜操作:这个阶段应该恢复UI状态(例如游戏状态)。<br>(3)回调方法:applicationWillResignActive:<br> 本地通知:UIApplicationWillResignActiveNotification<br> 触发时机:从活动状态进入非活动状态。<br> 适宜操作:这个阶段应该保存UI状态(例如游戏状态)。<br> (4)回调方法:applicationDidEnterBackground:</div>
<p>
本地通知:UIApplicationDidEnterBackgroundNotification<br> 触发时机:程序进入后台时调用。<br> 适宜操作:这个阶段应该保存用户数据,释放一些资源(例如释放数据库资源)。<br>(5)回调方法:applicationWillEnterForeground:<br> 本地通知:UIApplicationWillEnterForegroundNotification<br> 触发时机:程序进入前台,但是还没有处于活动状态时调用。<br> 适宜操作:这个阶段应该恢复用户数据。<br>(6)回调方法:applicationWillTerminate:<br> 本地通知:UIApplicationWillTerminateNotification<br> 触发时机:程序被杀死时调用。<br> 适宜操作:这个阶段应该进行释放一些资源和保存用户数据。<br> <br> 3、程序启动<br>点击应用图标时,会经历三个状态:<br>Not running-->Inactive-->Active <br><br>Not running -->Inactive<br>调用 application:didFinishLaunchingWithOptions: 发送:UIApplicationDidFinishLaunchingNotification <br>Inactive-->Active </p>
<div><br>调用 applicationDidBecomeActive: 发送:UIApplicationDidBecomeActiveNotification <br><br>4、程序Home<br>根据info.plist中Applicationdoes not run in background / UIApplicationExitsOnSuspend控制似乎否可以在后台运行或挂起。 <br>如果可以在后台运行或者挂起会经历<br>Active-->Inactive-->Background-->Suspended <br><br>Active-->Inactive <br>调用 applicationWillResignActive: 发送:UIApplicationWillResignActiveNotification <br>Background-->Suspended <br>调用 applicationDidEnterBackground: 发送:UIApplicationDidEnterBackgroundNotification <br><br>如果不可以后台运行或挂起会经历<br> Active-->Inactive-->Background-->Suspended-->NotRunning<br><br>Background-->Suspended <br>调用 applicationDidEnterBackground: 发送:UIApplicationDidEnterBackgroundNotification <br>Suspended-->Not Running <br>调用 applicationWillTerminate: 发送:UIApplicationWillTerminateNotification<br><br>5、挂起后,重新运行<br>Suspended-->Background-->Inactive-->Active<br><br> Background-->Inactive <br> 调用 applicationWillEnterForeground: 发送:UIApplicationWillEnterForegroundNotification <br> Inactive-->Active <br>调用 applicationDidBecomeActive: 发送:UIApplicationDidBecomeActiveNotification <br><br>6、内存不足,杀死程序<br> Background-->Suspended-->Notrunning<br>这种情况不会调用任何方法,也不会发送任何通知。</div>
<div> </div><br><br>
来源:https://www.cnblogs.com/hecanlin/p/12024390.html
頁:
[1]