app的生命周期

本文通过实验详细解析了iOS应用程序的生命周期,包括启动、暂停、进入后台、重新激活等关键阶段,并记录了各阶段对应的调用顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为了处理好应用程序的挂起、暂停等情况下的数据保存,或对应添加所需处理,我们必须了解ios生命周期。

但是不要去背去记,做个实验就好。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    NSLog(@"程序开始");
    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(@"程序暂停");
}

- (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(@"程序进入后台");
}

- (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(@"程序进入前台");
}

- (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(@"程序再次激活");
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    NSLog(@"程序意外终止");
}


实验结果:

1.首次启动应用程序:

2013-09-16 17:17:33.355 LifeCycle[2148:c07] 程序开始

2013-09-16 17:17:33.356 LifeCycle[2148:c07] 程序再次激活


2.摁HOME键退出:

2013-09-16 17:17:38.177 LifeCycle[2148:c07] 程序暂停

2013-09-16 17:17:38.178 LifeCycle[2148:c07] 程序进入后台


3.再次进入程序:

2013-09-16 17:17:52.738 LifeCycle[2148:c07] 程序进入前台

2013-09-16 17:17:52.739 LifeCycle[2148:c07] 程序再次激活


### 应用程序生命周期各阶段详解 #### 微信小程序生命周期 微信小程序的应用生命周期涵盖了从启动到关闭的全过程,开发者可以通过监听这些生命周期事件来实现资源管理和功能优化。以下是微信小程序的主要生命周期阶段及其对应的函数: 1. **应用初始化 (App Launch)** 当用户打开小程序时会触发 `onLaunch` 函数。此阶段主要用于全局变量初始化以及加载必要的数据[^1]。 2. **页面显示 (Page Show)** 用户进入某个具体页面时会触发 `onShow` 方法。该方法适用于处理页面重新展示的情况,比如从前台切换至后台再返回前台的情形。 3. **页面隐藏 (Page Hide)** 如果当前页面被遮挡或者跳转到了其他页面,则会调用 `onHide` 方法。这一步骤通常用来保存临时状态或停止某些耗电行为,例如暂停音频播放。 4. **应用卸载 (App Unload)** 小程序即将退出前会执行 `onUnload` 操作。这是清理工作的好时机,例如释放网络连接、存储未完成的数据等。 #### Vue.js 生命周期 Vue框架中的组件也遵循类似的生命周期模式,但更加细化于单个组件的行为控制上。下面列举了一些重要的Vue生命周期钩子并解释它们的作用: 1. **创建期 (Creation Phase):** - `beforeCreate`: 实例刚被创建出来之后立即调用,在这个阶段还无法访问DOM节点和data属性。 - `created`: 此时已经完成了响应式系统的设置,可以安全地操作data对象和其他选项配置[^2]。 2. **挂载期 (Mounting Phase):** - `beforeMount`: 组件模板编译成虚拟dom之前调用。 - `mounted`: 虚拟dom渲染完成后插入真实dom树中,此时可直接操纵真实的dom结构。 3. **更新期 (Updating Phase):** - `beforeUpdate`: 数据变更引起视图重绘之前的回调。 - `updated`: dom更新完毕后的动作集合点,注意这里可能会遇到一些异步问题所以要谨慎使用。 4. **销毁期 (Destruction Phase):** - `beforeDestroy`: 即将摧毁实例前最后的机会做些事情,像解绑自定义事件监听器之类的任务。 - `destroyed`: 所有绑定都已解除,所有的子组件也都已被销毁,最终结束整个流程。 通过合理利用上述各个周期内的特性,能够极大程度提高开发效率的同时保障良好的用户体验质量。 ```javascript // 示例代码:Vue组件基本生命周期演示 new Vue({ el: '#app', data() { return { message: 'Hello, world!' }; }, beforeCreate() { console.log('Before Create:', this.$el); // undefined }, created() { console.log('Created:', this.message); }, beforeMount() { console.log('Before Mount:'); }, mounted() { console.log('Mounted:', document.getElementById('output').textContent); } }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值