iOS study notes (4)-iOS application life cycle iOS application life cycle (front and back switching, various states of the application) detailed explanation

iOS Application Life Cycle

Application status

There are 5 states in IOS application.

  • Not running: The program is not started
  • Inactive: The transient state that appears when the other two states switch. The only time to stay in this state for a long time is when the user locks the screen? Or the system prompts the user to respond to the Alert window (such as incoming calls, messages)
  • Active: The normal operating state displayed on the screen , in which user input can be received and the display can be updated
  • Backgroud: The program is in the background and can execute code. The user enters this state shortly after pressing the Home button (first enters the Inactive state, then enters the Background state), and then quickly enters the Suspended state. Some programs can stay in Backgroud state for a long time after special request
  • Suspended: The program cannot execute code in the background. Ordinary programs will enter this state shortly after entering the Background state. When suspended, the program still stays in the memory. When the system memory is low, the system clears the suspended program to provide more memory for the foreground program

This diagram is very important, and every arrow needs to be studied carefully.

About the switch between Active and Inactive:

When the application is in the foreground, there are two states: Active and Inactive. In most cases, the Inactive state is just a transient state that appears when the other two states are switched (not that the switch between any two states will enter Inactive, as shown in the figure). If you open the application, it will enter Inactive from Not Running first and then enter Active; For example, when the front and back applications are switched, Inactive will briefly appear between Active and Background.

But there are other situations. Active and Inactive can be switched when running in the foreground. For example, when the system pops up Alert, the application will switch from Active to Inactive until the user confirms and then return to Actvie. If the user pulls down the notification page, Active will also occur. Switching with Inactive; there are incoming calls but rejected, double-clicking the Home button but returning to the original application, etc. do not enter the Background, but only switch between Active and Inactive.

As shown in the figure, can you say that if you want to enter Active, you must enter Inactive first.

Entry function

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类六个方法

注意代码中的官方注释。

  View Code

 

启动程序

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应用程序的状态及其切换(生命周期)

转载请注明出处http://www.cnblogs.com/chenyg32/
分类:  iOS
1
0
(请您对文章做出评价)
« 上一篇: 简单的TableView
» 下一篇: iOS ViewController生命周期
posted @  2014-07-28 16:01  Norcy 阅读( 5196) 评论( 0)   编辑  收藏

Guess you like

Origin blog.csdn.net/qq_27740983/article/details/50844044