当前位置:网站首页>[Android, kotlin] teaches you to write a weak reference handler tool to avoid memory leakage
[Android, kotlin] teaches you to write a weak reference handler tool to avoid memory leakage
2022-04-21 15:59:00 【mozhimen】
Teach you to write a weak quote Handler Tool class , Avoid memory leaks
Crazy Coding
UtilKHandler
open class UtilKHandler<T>(cls: T) : Handler(Looper.getMainLooper()) {
var ref: WeakReference<T>? = null
init {
ref = WeakReference(cls)
}
/**
* obtain UIHandler
* @return T?
*/
fun getRef(): T? {
return ref?.get()
}
}
How To Use
UtilKHandler(this@RefreshKLayoutActivity).postDelayed(1000) { vb.refreshkContainer.refreshFinished() }
You can also write an extension function , Use it with
/**
* postDelay
* @receiver Handler
* @param delayMills Long
* @param runnable Runnable
*/
fun Handler.postDelayed(delayMills: Long, runnable: Runnable) {
this.postDelayed(runnable, delayMills)
}
/**
* Get to the head of the team
* @receiver Handler
* @param runnable Runnable
*/
fun Handler.sendAtFrontOfQueue(runnable: Runnable) {
val msg = Message.obtain(this, runnable)
this.sendMessageAtFrontOfQueue(msg)
}
/**
* remove
* @receiver Handler
* @param runnable Runnable
*/
fun Handler.remove(runnable: Runnable) {
this.removeCallbacks(runnable)
}
版权声明
本文为[mozhimen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211546492325.html
边栏推荐
- 全国查询水电气费免费接口(二)
- 6 IMPORTANT JOB SKILLS YOU NEED AS A NETWORK CABLE TECHNICIAN
- 季更43/90
- 小米Civi 1S 定价2299元起,主打美颜,让你上镜自由
- 金仓数据库KingbaseES V8R3集群在线删除数据节点案例
- R语言manova函数稳健多元方差分析(Robust one-way MANOVA)、rrcov包中的wilks.test函数稳健单向MANOVA、vegan包的adonis函数非参数Manova等效
- Transformer模型技术长文
- 软件测试mysql面试题:用SQL建立表并且筛选?
- 嵌入式GUI盘点-你了解几款?
- LeetCode 567、字符串的排列
猜你喜欢

Xiaomi Hongmi's note 4x brush machine succeeded

Announcement of the first ship sea data intelligent application innovation competition

Correspondence between annotation of CDs view and UI elements in SAP Fiori smart template technology

柱状图应用全面剖析

免费下载文档教程

ABAP关键字AT FIRST和AT LAST学习

Web. Detailed explanation of XML file

.NET Swagger配置

Xiaomi civi 1s is priced from 2299 yuan. It focuses on beauty and gives you freedom to appear on the camera

LeetCode 141、环形链表
随机推荐
C语言进阶第41式:内存操作经典问题分析一
做程序员为什么这么累?
Programmer meituan interview experience, from basic to algorithm, lasted six hours, with an annual salary of 20W
必刷|2022年重庆最新八大员之(安全员)模拟题库及答案
.NET Swagger配置
AnalyticDB PostgreSQL新版云原生让用户专注于业务,实现怎样的体验升级?
PHP 零基础入门笔记(11):字符串 String
【2023校招刷題】華為性格測評(綜合測評)戰略指南
Ji Geng 57 / 90
Establishment of instant messaging system and development of IM chat social software
全国查询水电气费免费接口(三)
魅族官方刷机教程地址(免费)
Lightly:新一代的 PHP IDE
季更43/90
金仓数据库KingbaseES V8R3集群修改data路径测试案例
小米 红米版note 4x刷机成功
首届船海数据智能应用创新大赛赛事公告
干货 | 解决 App 自动化测试的常见痛点(弹框及首页启动加载完成判断处理)
Qt5.14.2编译mysql
c# 坐标点击webBrowser1