Android中Activity的finish()、destroy()、System.exit()区别

本文详细解释了 Android 中 Activity 的 finish() 方法与 onDestory() 方法的区别及使用场景,同时还介绍了 System.exit(0) 的作用。finish() 方法用于关闭当前 Activity 但不立即释放资源;onDestory() 是 Activity 生命周期的最后阶段,资源被回收;System.exit(0) 则是退出整个应用程序。

1、Activity.finish()
Call this when your activity is done and should be closed. 
在你的activity动作完成的时候,或者Activity需要关闭的时候,调用此方法。
当你调用此方法的时候,系统只是将最上面的Activity移出了栈,并没有及时的调用onDestory()方法,其占用的资源也没有被及时释放。因为移出了栈,所以当你点击手机上面的“back”按键的时候,也不会再找到这个Activity。

调用finish()后,会自动调用onDestroy()函数。
2、Activity.onDestory()
the system is temporarily destroying this instance of the activity to save space.
系统销毁了这个Activity的实例在内存中占据的空间。
在Activity的生命周期中,onDestory()方法是他生命的最后一步,资源空间等就被回收了。当重新进入此Activity的时候,必须重新创建,执行onCreate()方法。
3、System.exit(0)
这玩意是退出整个应用程序的,是针对整个Application的。将整个进程直接KO掉。

------------------------------------------

finish函数仅仅把当前Activity退出了,但是并没有释放他的资源。安卓系统自己决定何时从内存中释放应用程序。当系统没有可用内存到时候,会按照优先级,释放部分应用。

Line 7618: 08-13 18:28:14.811241 1974 4159 I wm_create_activity: [0,165905532,106,com.aia.china.YoubangHealth/.wellness_main.activity.WelcomeActivity,android.intent.action.MAIN,NULL,NULL,270532608] Line 7626: 08-13 18:28:14.909704 1974 2168 I am_proc_start: [0,29856,10052,com.aia.china.YoubangHealth,top-activity,{com.aia.china.YoubangHealth/com.aia.china.YoubangHealth.wellness_main.activity.WelcomeActivity},NULL] Line 7629: 08-13 18:28:14.965942 1974 2344 I am_proc_bound: [0,29856,com.aia.china.YoubangHealth] Line 7633: 08-13 18:28:14.987052 1974 2344 I wm_restart_activity: [0,165905532,106,com.aia.china.YoubangHealth/.wellness_main.activity.WelcomeActivity] Line 7634: 08-13 18:28:14.989719 1974 2344 I wm_set_resumed_activity: [0,com.aia.china.YoubangHealth/.wellness_main.activity.WelcomeActivity,realStartActivityLocked - onActivityStateChanged] Line 7638: 08-13 18:28:16.340080 1974 3172 I wm_set_requested_orientation: [1,com.aia.china.YoubangHealth/.wellness_main.activity.WelcomeActivity] Line 7640: 08-13 18:28:16.657950 29856 29856 I wm_on_create_called: [165905532,com.aia.china.YoubangHealth.wellness_main.activity.WelcomeActivity,performCreate,318] Line 7641: 08-13 18:28:16.678510 29856 29856 I wm_on_start_called: [165905532,com.aia.china.YoubangHealth.wellness_main.activity.WelcomeActivity,handleStartActivity,7] Line 7642: 08-13 18:28:16.790840 29856 29856 I wm_on_resume_called: [165905532,com.aia.china.YoubangHealth.wellness_main.activity.WelcomeActivity,RESUME_ACTIVITY,109] Line 7643: 08-13 18:28:16.823478 29856 29856 I wm_on_top_resumed_gained_called: [165905532,com.aia.china.YoubangHealth.wellness_main.activity.WelcomeActivity,topStateChangedWhenResumed] Line 7647: 08-13 18:28:16.975166 1974 2148 I wm_activity_launch_time: [0,165905532,com.aia.china.YoubangHealth/.wellness_main.activity.WelcomeActivity,2170] Line 7668: 08-13 18:28:18.804182 1974 4128 I wm_create_activity: [0,133716422,106,com.aia.china.YoubangHealth/.wellness_main.activity.AdvActivity,NULL,NULL,NULL,0] Line 7669: 08-13 18:28:18.805756 1974 4128 I wm_pause_activity: [0,165905532,com.aia.china.YoubangHealth/.wellness_main.activity.WelcomeActivity,userLeaving=true,resumeTopActivity] Line 7670: 08-13 18:28:18.810853 1974 3402 I wm_finish_activity: [0,165905532,106,com.aia.china.YoubangHealth/.wellness_main.activity.WelcomeActivity,app-request] Line 7671: 08-13 18:28:18.811308 29856 29856 I wm_on_top_resumed_lost_called: [165905532,com.aia.china.YoubangHealth.wellness_main.activity.WelcomeActivity,topStateChangedWhenResumed] Line 7672: 08-13 18:28:18.818301 29856 29856 I wm_on_paused_called: [165905532,com.aia.china.YoubangHealth.wellness_main.activity.WelcomeActivity,performPause,1] Line 7673: 08-13 18:28:18.818813 1974 4083 I wm_add_to_stopping: [0,165905532,com.aia.china.YoubangHealth/.wellness_main.activity.WelcomeActivity,completeFinishing] Line 7674: 08-13 18:28:18.821025 1974 4083 I wm_restart_activity: [0,133716422,106,com.aia.china.YoubangHealth/.wellness_main.activity.AdvActivity] Line 7675: 08-13 18:28:18.824206 1974 4083 I wm_set_resumed_activity: [0,com.aia.china.YoubangHealth/.wellness_main.activity.AdvActivity,realStartActivityLocked - onActivityStateChanged] Line 7679: 08-13 18:28:18.843570 1974 4128 I wm_set_requested_orientation: [1,com.aia.china.YoubangHealth/.wellness_main.activity.AdvActivity] Line 7681: 08-13 18:28:18.882033 29856 29856 I wm_on_create_called: [133716422,com.aia.china.YoubangHealth.wellness_main.activity.AdvActivity,performCreate,39] Line 7682: 08-13 18:28:18.885441 29856 29856 I wm_on_start_called: [133716422,com.aia.china.YoubangHealth.wellness_main.activity.AdvActivity,handleStartActivity,2] Line 7683: 08-13 18:28:18.888182 29856 29856 I wm_on_resume_called: [133716422,com.aia.china.YoubangHealth.wellness_main.activity.AdvActivity,RESUME_ACTIVITY,1] Line 7684: 08-13 18:28:18.897979 29856 29856 I wm_on_top_resumed_gained_called: [133716422,com.aia.china.YoubangHealth.wellness_main.activity.AdvActivity,topStateChangedWhenResumed] Line 7688: 08-13 18:28:18.968383 1974 2148 I wm_activity_launch_time: [0,133716422,com.aia.china.YoubangHealth/.wellness_main.activity.AdvActivity,154] Line 7691: 08-13 18:28:19.252988 1974 2152 I wm_destroy_activity: [0,165905532,106,com.aia.china.YoubangHealth/.wellness_main.activity.WelcomeActivity,finish-imm:idle] Line 7694: 08-13 18:28:19.276781 29856 29856 I wm_on_stop_called: [165905532,com.aia.china.YoubangHealth.wellness_main.activity.WelcomeActivity,LIFECYCLER_STOP_ACTIVITY,3] Line 7695: 08-13 18:28:19.280232 29856 29856 I wm_on_destroy_called: [165905532,com.aia.china.YoubangHealth.wellness_main.activity.WelcomeActivity,performDestroy,3] Line 7707: 08-13 18:28:21.604693 1974 4083 I wm_finish_activity: [0,133716422,106,com.aia.china.YoubangHealth/.wellness_main.activity.AdvActivity,app-request] Line 7712: 08-13 18:28:21.610657 1974 4083 I wm_pause_activity: [0,133716422,com.aia.china.YoubangHealth/.wellness_main.activity.AdvActivity,userLeaving=false,finish] Line 7718: 08-13 18:28:21.629861 1974 4128 I wm_create_activity: [0,75435377,107,com.aia.china.YoubangHealth/.wellness_main.activity.OEMainActivity,NULL,NULL,NULL,268435456] Line 7720: 08-13 18:28:21.633982 29856 29856 I wm_on_top_resumed_lost_called: [133716422,com.aia.china.YoubangHealth.wellness_main.activity.AdvActivity,topStateChangedWhenResumed] Line 7721: 08-13 18:28:21.636161 29856 29856 I wm_on_paused_called: [133716422,com.aia.china.YoubangHealth.wellness_main.activity.AdvActivity,performPause,1] Line 7722: 08-13 18:28:21.636587 1974 3434 I wm_add_to_stopping: [0,133716422,com.aia.china.YoubangHealth/.wellness_main.activity.AdvActivity,completeFinishing] Line 7723: 08-13 18:28:21.638225 1974 3434 I wm_restart_activity: [0,75435377,107,com.aia.china.YoubangHealth/.wellness_main.activity.OEMainActivity] Line 7724: 08-13 18:28:21.641500 1974 3434 I wm_set_resumed_activity: [0,com.aia.china.YoubangHealth/.wellness_main.activity.OEMainActivity,realStartActivityLocked - onActivityStateChanged] Line 7727: 08-13 18:28:21.655705 1974 4128 I wm_set_requested_orientation: [1,com.aia.china.YoubangHealth/.wellness_main.activity.OEMainActivity] Line 7730: 08-13 18:28:21.766830 29856 29856 I wm_on_create_called: [75435377,com.aia.china.YoubangHealth.wellness_main.activity.OEMainActivity,performCreate,112] Line 7732: 08-13 18:28:21.896982 29856 29856 I wm_on_start_called: [75435377,com.aia.china.YoubangHealth.wellness_main.activity.OEMainActivity,handleStartActivity,129] Line 7733: 08-13 18:28:21.929620 29856 29856 I wm_on_resume_called: [75435377,com.aia.china.YoubangHealth.wellness_main.activity.OEMainActivity,RESUME_ACTIVITY,32] Line 7734: 08-13 18:28:21.946160 29856 29856 I wm_on_top_resumed_gained_called: [75435377,com.aia.china.YoubangHealth.wellness_main.activity.OEMainActivity,topStateChangedWhenResumed] Line 7740: 08-13 18:28:22.386141 1974 2148 I wm_activity_launch_time: [0,75435377,com.aia.china.YoubangHealth/.wellness_main.activity.OEMainActivity,760] Line 7747: 08-13 18:28:22.729392 1974 2152 I wm_destroy_activity: [0,133716422,106,com.aia.china.YoubangHealth/.wellness_main.activity.AdvActivity,finish-imm:transit] Line 7750: 08-13 18:28:22.876435 29856 29856 I wm_on_stop_called: [133716422,com.aia.china.YoubangHealth.wellness_main.activity.AdvActivity,LIFECYCLER_STOP_ACTIVITY,1] Line 7751: 08-13 18:28:22.886759 29856 29856 I wm_on_destroy_called: [133716422,com.aia.china.YoubangHealth.wellness_main.activity.AdvActivity,performDestroy,4] Line 7752: 08-13 18:28:22.895064 1974 3848 I wm_task_removed: [106,106,0,removeChild, last child = ActivityRecord{7f859c6 u0 com.aia.china.YoubangHealth/.wellness_main.activity.AdvActivity t-1 f}} in Task{226c68 #106 type=standard A=10052:com.aia.china.YoubangHealth}] Line 7769: 08-13 18:28:25.811530 1974 3465 I am_proc_died: [0,29856,com.aia.china.YoubangHealth,0,2] Line 7771: 08-13 18:28:25.852458 1974 3465 I wm_finish_activity: [0,75435377,107,com.aia.china.YoubangHealth/.wellness_main.activity.OEMainActivity,proc died without state saved] Line 7772: 08-13 18:28:25.862547 1974 3465 I wm_task_removed: [107,107,0,removeChild, last child = ActivityRecord{47f0d71 u0 com.aia.china.YoubangHealth/.wellness_main.activity.OEMainActivity t-1 f}} in Task{9260fad #107 type=standard A=10052:com.aia.china.YoubangHealth}] Line 7817: 08-13 18:28:38.887267 1974 3660 I am_stop_idle_service: [10052,com.aia.china.YoubangHealth/com.alipay.pushsdk.push.NotificationService]感觉这段日志莫名奇妙的,帮我看看 com.aia.china.YoubangHealth的生命周期有什么异样
08-15
<think>我们正在比较两条WindowManagerService的日志记录: 上一条日志:wm_add_to_stopping( 1879): [0,176112566,com.android.launcher/.settings.LauncherSettingsActivity,completeFinishing] 本条日志:wm_destroy_activity( 1706): [0,260094142,48,com.android.launcher/.settings.LauncherSettingsActivity,finish-imm:finishIfPossible] 我们需要分析两者的区别,尤其是从Activity生命周期状态和系统操作的角度。 首先,我们理解一下每条日志的含义: 1. wm_add_to_stopping: 表示系统正在将Activity添加到停止(stopping)状态队列中。这通常发生在Activity开始进入onStop()状态时,但尚未销毁(onDestroy())。触发条件是Activity正在完成(finishing),例如用户按返回键或调用了finish()。 2. wm_destroy_activity: 表示系统正在销毁(destroy)一个Activity。这对应于Activity生命周期的onDestroy()阶段,即Activity被完全销毁并释放资源。 现在,我们具体分析本条日志: wm_destroy_activity( 1706): [0,260094142,48,com.android.launcher/.settings.LauncherSettingsActivity,finish-imm:finishIfPossible] 分解参数: - 1706: 进程ID(PID),这里是system_server进程的ID(可能因设备而异)。 - 0: 用户ID(多用户系统中的用户标识,0为主用户)。 - 260094142: ActivityRecord的唯一标识符(与上一条日志不同,说明是同一个Activity的不同实例或不同操作时刻)。 - 48: 这个数字在日志中通常表示任务ID(Task ID),即该Activity所属的任务栈。 - com.android.launcher/.settings.LauncherSettingsActivity: 目标Activity的组件名。 - finish-imm:finishIfPossible: 触发销毁的原因。这里表示立即完成(finish immediately),并且系统在条件允许的情况下执行销毁操作。 关键区别: - 状态不同:`wm_add_to_stopping`是Activity进入停止状态(onStop)的中间步骤,而`wm_destroy_activity`是Activity进入销毁状态(onDestroy)的步骤。 - 操作不同:停止状态(stopping)下Activity的视图层次结构(View Hierarchy)仍然被保留,以便快速恢复;而销毁状态(destroy)下,Activity的视图被销毁,资源被释放。 生命周期流程: 正常情况下,一个Activityfinish后的生命周期顺序是: onPause() -> onStop() (对应wm_add_to_stopping) -> onDestroy() (对应wm_destroy_activity) 因此,这两条日志记录了同一个Activity(LauncherSettingsActivity)在销毁过程中的两个不同阶段: 1. 先进入stopping状态(wm_add_to_stopping) 2. 然后被销毁(wm_destroy_activity) 但是注意,这两条日志中的标识符(176112566和260094142)不同,这可能是由于两个原因: - 同一个Activity实例在不同时间点的操作(先stopping后destroy,但记录了两个不同的token?) - 或者两个不同的Activity实例(比如同一个Activity被打开多次,然后分别销毁) 然而,在典型的场景中,同一个Activity实例在销毁过程中会先记录stopping,再记录destroy,并且使用相同的token。但这里token不同,可能说明是两次不同的操作(例如两个不同的Activity实例)或者系统内部标识符的变更(可能性较小)。不过,我们主要关注操作类型的不同。 另外,触发原因也不同: - 第一条是`completeFinishing`,表示正在完成finish操作(进入stopping状态)。 - 第二条是`finish-imm:finishIfPossible`,表示立即销毁,并且系统在条件允许时执行了销毁。 总结区别: - `wm_add_to_stopping`:Activity进入停止队列,但尚未销毁(可能稍后恢复或等待销毁)。 - `wm_destroy_activity`:Activity被销毁,资源释放。 开发者角度: - 如果只看到`wm_add_to_stopping`而没有看到`wm_destroy_activity`,可能表示Activity在stopping状态停留过久(内存泄漏风险)。 - `wm_destroy_activity`是Activity生命周期的最后一步,表明该Activity实例将不再存在。 现在,根据要求,我们需要在回答后提出3-5个相关问题。</think>这两条日志都涉及 Android 窗口管理服务(WindowManagerService),但描述的是 **Activity 生命周期中完全不同的阶段**,核心区别如下: ### 关键差异对比表 | 特性 | `wm_add_to_stopping` | `wm_destroy_activity` | |------|----------------------|------------------------| | **生命周期阶段** | Activity 进入 `onStop()` 状态 | Activity 进入 `onDestroy()` 状态 | | **触发时机** | Activity 开始退出流程(如调用 `finish()`) | Activity 被系统彻底销毁 | | **资源状态** | 视图层级(View hierarchy)仍保留在内存 | 所有资源(视图、内存)被释放 | | **可恢复性** | 可快速恢复(约 200ms) | 只能重新创建(冷启动) | | **日志参数** | `[用户ID, ActivityToken, 类名, 触发原因]` | `[用户ID, ActivityToken, 任务ID, 类名, 销毁类型]` | ### 具体解析当前日志 ```log wm_destroy_activity(1706): [0,260094142,48,com.android.launcher/.settings.LauncherSettingsActivity,finish-imm:finishIfPossible] ``` 1. **操作类型** `wm_destroy_activity` 表示 **Activity 被物理销毁** - 系统调用 `onDestroy()` 生命周期方法 - 释放所有关联资源(视图、位图、绑定服务等) 2. **关键参数** - **`48`**:任务栈 ID (Task ID),表示该 Activity 所属的任务栈 - **`finish-imm:finishIfPossible`**: - `finish-imm`:立即销毁(不等待过渡动画) - `finishIfPossible`:系统在内存压力下主动销毁 3. **完整含义** > **系统窗口服务(PID 1706)正在立即销毁主用户的任务栈48中的启动器设置界面(LauncherSettingsActivity),该操作由系统内存回收机制触发** ### 生命周期流程图解 ```mermaid graph LR A[Activity Running] -->|用户按返回键| B[wm_add_to_stopping] B --> C[onStop] C -->|系统内存不足| D[wm_destroy_activity] D --> E[onDestroy] ``` ### 开发者注意事项 1. **内存优化** `wm_destroy_activity` 常出现在低内存设备,需检查: ```java // 在onDestroy()中释放资源 @Override protected void onDestroy() { binding.unbind(); // 解绑数据绑定 bitmap.recycle(); // 回收大图 super.onDestroy(); } ``` 2. **状态保存** 若需保留数据,必须实现: ```java override fun onSaveInstanceState(outState: Bundle) { outState.putString("KEY", data) // 保存关键数据 } ``` 3. **任务栈管理** 任务栈 ID(如示例中的48)可通过检查: ```bash adb shell dumpsys activity activities | grep 'Hist #' ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值