当前位置:网站首页>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非空的意思然后就解除警告,详细讲解参考里面有,就不复制粘贴了,只贴了代码和效果图
边栏推荐
- STC 32比特8051單片機開發實例教程 一 開發環境搭建
- The author believes that the so-called industrial Internet is a process of deep integration of industry and the Internet
- Wechat applet: wechat can also send flash photos to create wechat applet source code download and customize flash time
- Lihongyi, machine learning 5 Tips for neural network design
- Shifu, the open source development platform of the Internet of things, is open for internal testing! Release of the first version of technical documents
- 51万奖池邀你参战——第二届阿里云ECS CloudBuild开发者大赛来袭
- [image fusion] sparse regularization based on non convex penalty to realize image fusion with matlab code
- PAT 乙等 1016 C语言
- What if win11 cannot record audio? Solution of win11 unable to input sound
- [proteus simulation] Arduino uno+pcf8574+lcd1602+mpx4250 electronic scale
猜你喜欢

iNFTnews | 加密之家从宇宙寄来的明信片,你会收到哪一张?

数字藏品赋能实体产业释放了哪些利好?

MDM data cleaning function development description

GDB data reading in GDAL (III) of GIS

Behind the hot digital collections, a strong technical team is needed to support the northern technical team

Real MySQL interview question (XXVIII) -- case - Analysis of indicators of communication operators

MySQL面试真题(二十一)——金融-贷款逾期

The 510000 prize pool invites you to participate in the competition -- the second Alibaba cloud ECS cloudbuild developer competition is coming

Wechat applet: Star Trek spaceship ticket production and generation

高等数学(第七版)同济大学 习题1-8 个人解答
随机推荐
The author believes that the so-called industrial Internet is a process of deep integration of industry and the Internet
Leetcode 797: all possible paths
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-9 personal solutions
ArcTime 制作中英文字幕视频
Heimdall database proxy scale out 20 times
What if win11 cannot record audio? Solution of win11 unable to input sound
Composite API
Lihongyi, machine learning 5 Tips for neural network design
Use of visdom
【owt】owt-client-native-p2p-e2e-test vs2017构建 6:修改脚本自动生成vs工程
opencv显示图像
数字藏品赋能实体产业释放了哪些利好?
SIFT feature point extraction
Wechat applet: a new interesting test
Oracle异常
Ip6809 three coil 15W wireless charging transmitter scheme IC British chip
Jsonfield annotation in fastjson
How to move the software downloaded from win11 app store to the desktop
visdom的使用
IP6809三线圈15W无线充电发射端方案ic英集芯
