当前位置:网站首页>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非空的意思然后就解除警告,详细讲解参考里面有,就不复制粘贴了,只贴了代码和效果图
边栏推荐
- MySQL面试真题(二十二)——表连接后的条件筛选及分组筛选
- AI艺术的基因工程?使用 #Artbreeder 改变图像的任意形态
- Ip6809 three coil 15W wireless charging transmitter scheme IC British chip
- Fs2119a Synchronous Boost IC output 3.3V and fs2119b Synchronous Boost IC output 5V
- 基于SSM框架的借阅图书管理系统
- Current situation and development of containerization technology under the cloud native trend
- MySQL character set
- Jvm: when a method is overloaded, the specific method to call is determined by the static type of the incoming parameter rather than the actual type of the parameter
- Is there a real part-time job online? How do college students find part-time jobs in summer?
- Yingjixin ip6806 wireless charging scheme 5W Qi certified peripheral simplified 14 devices
猜你喜欢
![[opencv450] image subtraction, binarization, threshold segmentation](/img/2c/60d64f3cdcc4ad8f95369d30ad2cdd.png)
[opencv450] image subtraction, binarization, threshold segmentation

Leetcode 797: all possible paths

Arctime makes Chinese and English subtitle video

雷达图canvas

C primer plus learning notes - 2. Constant and formatted IO (input / output)

runc 符号链接挂载与容器逃逸漏洞预警(CVE-2021-30465)

GDB data reading in GDAL (III) of GIS

MySQL面试真题(二十七)——RFM分析法对用户进行分类

制造业数字化转型存在问题及原因分析

Jvm: when a method is overloaded, the specific method to call is determined by the static type of the incoming parameter rather than the actual type of the parameter
随机推荐
Wechat applet: a new interesting test
Heimdall Database Proxy横向扩展提高20倍
GDB data reading in GDAL (III) of GIS
Redis缓存穿透解决方案-布隆过滤器
制造业数字化转型存在问题及原因分析
技能自检 | 想当测试Leader,这6项技能你会吗?
Win11如何开启移动热点?Win11开启移动热点的方法
C primer plus學習筆記 —— 2、常量與格式化IO(輸入/輸出)
Wechat applet: Star Trek spaceship ticket production and generation
The performance of nonstandard sprintf code in different platforms
Win11 app store keeps turning around solution
C primer plus learning notes - 2. Constant and formatted IO (input / output)
Wechat applet: future wife query generator
基于SSM框架的借阅图书管理系统
移动电源快充QC3.0方案芯片IP5318快充方案
Build a gocd environment
Yingjixin ip6806 wireless charging scheme 5W Qi certified peripheral simplified 14 devices
AHA C language Chapter 8 game time is up (lesson 29)
Redis cache penetration solution - bloom filter
高等数学(第七版)同济大学 习题1-8 个人解答
