当前位置:网站首页>android Handler内存泄露 kotlin内存泄露处理
android Handler内存泄露 kotlin内存泄露处理
2022-06-23 04:14:00 【木子102】
这几天使用kotlin写一个项目,用到handler,然后提醒提警告
This Handler class should be static or leaks might occur......
I wrote that debugging code because of a couple of memory leaks I
found in the Android codebase. Like you said, a Message has a
reference to the Handler which, when it's inner and non-static, has a
reference to the outer this (an Activity for instance.) If the Message
lives in the queue for a long time, which happens fairly easily when
posting a delayed message for instance, you keep a reference to the
Activity and "leak" all the views and resources. It gets even worse
when you obtain a Message and don't post it right away but keep it
somewhere (for instance in a static structure) for later use. 百度后才发现是handler内存泄露的问题(原谅小木子是菜鸟)
参考:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1106/1922.html
http://blog.csdn.net/donkor_/article/details/78796518?utm_source=tuicool&utm_medium=referral
原代码
private var mHandler1: Handler = object : Handler() {
override fun handleMessage(msg: Message?) {
super.handleMessage(msg)
if (msg != null) {
when (msg.what) {
CKey.WHAT_TWO -> {
verify_tv_again.text = "获取验证码"
verify_tv_time.text = ""
verify_next.isEnabled = true
verify_tv_again.isEnabled = true
goToAccountInfoActivity()
}
}
}
}
}然后按照网站的提示,做了修改:
private class MyHandler(activity: VerifyActivityActivity) : Handler() {
private val mActivity: WeakReference<VerifyActivityActivity> = WeakReference(activity)
override fun handleMessage(msg: Message) {
if (mActivity.get() == null) {
return
}
val activity = mActivity.get()
when (msg.what) {
102 -> {
activity!!.verify_tv_again.text = "李振华"
activity.verify_tv_time.text = ""
activity.verify_next.isEnabled = true
activity.verify_tv_again.isEnabled = true
// activity.goToAccountInfoActivity()
}
else -> {
}
}
}
}
初始化参数的时候声明一个即可使用(黄色背景即有警告 ,修改后没有了黄色警告)
var handler12 = MyHandler(this)
handler12.sendEmptyMessage(102)效果

大致讲解:
首先是拿到activity的弱引用 private val mActivity: WeakReference = WeakReference(activity)
然后在需要修改的view之间使用这个引用查找响应的控件做UI修改即可
activity!!.verify_tv_again.text = “李振华”
activity.verify_tv_time.text = “”
activity.verify_next.isEnabled = true
activity.verify_tv_again.isEnabled = true
!!是kotlin非空的意思然后就解除警告,详细讲解参考里面有,就不复制粘贴了,只贴了代码和效果图
边栏推荐
- PAT 乙等 1018 C语言
- PAT 乙等 1025 反转链表
- MDM数据清洗功能开发说明
- MySQL面试真题(二十二)——表连接后的条件筛选及分组筛选
- 1010 Radix
- The performance of nonstandard sprintf code in different platforms
- Yingjixin ip6806 wireless charging scheme 5W Qi certified peripheral simplified 14 devices
- Advanced Mathematics (Seventh Edition) Tongji University exercises 1-8 personal solutions
- @jsonfield annotation in fastjson
- PAT 乙等 1011 C语言
猜你喜欢

Yingjixin ip6806 wireless charging scheme 5W Qi certified peripheral simplified 14 devices

sprintf 格式代码使用不规范在不同平台下的表现

51万奖池邀你参战——第二届阿里云ECS CloudBuild开发者大赛来袭

Fs2119a Synchronous Boost IC output 3.3V and fs2119b Synchronous Boost IC output 5V

True question of MySQL interview (29) -- case - finding favorite movies

Genetic engineering of AI art? Use # artbreeder to change any shape of the image

Lottery DDD code

How does win11 enable mobile hotspot? How to enable mobile hotspot in win11

阿里云对象存储oss+picgo+typora实现构建图床

云原生数据库是未来
随机推荐
June 22, 2022: golang multiple choice question, what does the following golang code output? A:3; B:1; C:4; D: Compilation failed. package main import ( “fmt“ ) func mai
How does win11 enable mobile hotspot? How to enable mobile hotspot in win11
树莓派assert初步使用练习
Win11 app store keeps turning around solution
STC 32比特8051單片機開發實例教程 一 開發環境搭建
ssm项目搭建
Heimdall Database Proxy横向扩展提高20倍
数字藏品如何赋能经济实体?
Pkav simple blasting
PAT 乙等 1022 D进制的A+B
【数据库备份】通过定时任务完成MySQL数据库的备份
CF [1700d] D. River locks (DP, bisection, Mathematics)
The digital collection market has just begun
MySQL面试真题(二十一)——金融-贷款逾期
AI艺术的基因工程?使用 #Artbreeder 改变图像的任意形态
fastjson中的@JSONField注解
Wechat applet: Star Trek spaceship ticket production and generation
IP6809三线圈15W无线充电发射端方案ic英集芯
抽奖 ddd 代码
AHA C language Chapter 7 you can do more with it (talks 27-28)
