android 什么时候call super.onDestory()等

本文详细解析了在Android应用开发中,如何正确地覆盖组件的生命周期方法。包括在组件创建阶段,如onCreate(), onStart(), onResume()等,应首先调用superclass确保Android系统的初始化工作完成;在组件销毁阶段,如onPause(), onStop(), onDestroy()等,应先执行自定义的清理工作再调用superclass。此外,对于返回非void类型的方法,如onCreateOptionsMenu(),通常在返回语句中调用superclass,除非需要强制特定的返回值。

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

Methods you override that are part of component creation (onCreate()onStart()onResume(), etc.), you should chain to the superclass as the first statement, to ensure that Android has its chance to do its work before you attempt to do something that relies upon that work having been done.

Methods you override that are part of component destruction (onPause()onStop()onDestroy(), etc.), you should do your work first and chain to the superclass as the last thing. That way, in case Android cleans up something that your work depends upon, you will have done your work first.

Methods that return something other than void (onCreateOptionsMenu(), etc.), sometimes you chain to the superclass in the return statement, assuming that you are not specifically doing something that needs to force a particular return value.

Everything else -- such as onActivityResult() -- is up to you, on the whole. I tend to chain to the superclass as the first thing, but unless you are running into problems, chaining later should be fine.

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

during any kind of initialization, let the super class do their work first; during any kind of finalization, you do your work first

转载于:https://www.cnblogs.com/Sir-Lin/p/6408503.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值