当前位置:网站首页>Kotlin Android simple activity jump, simple combination of handler and thread
Kotlin Android simple activity jump, simple combination of handler and thread
2022-06-23 05:52:00 【Muzi 102】
kotlin The future android Development programming trends , As a salted fish , Beginners kotlin note
- establish kotlin Layout :(androidstudio3.0 Software already supported kotlin,3.0 The following needs to be installed kotlin plug-in unit , You can install it by yourself )

2. Initialize control ( Very convenient , Unwanted findviewbyid, Just use the address reference directly , But make sure the address is unique )
main_tv_hello.text = "nxm"
3. Declare variables and initialize variables ( Initialize when a variable is required , This section initializes handler and thread As an example )
// Declare variables
private lateinit var handler: Handler
private lateinit var myThread: Thread
// Initialize variable
fun initData() {
// initialization myThread
myThread = object : Thread() {
override fun run() {
super.run()
handler.sendEmptyMessageDelayed(Constants.ZERO, 2000)
}
}
// initialization handler
handler = object : Handler() {
override fun handleMessage(msg: Message?) {
super.handleMessage(msg)
if (msg?.what == Constants.ZERO) {
var intent = Intent()
intent.setClass(this@Wellcome, MainActivity::class.java)
startActivity(intent)
}
}
}
}4.0 above handler Rewrote handleMessage Method , There is a method to jump to the interface ( Kiko has searched many websites this Most quotes say [email protected], It turned out to be wrong [email protected])
//Intent Page Jump
var intent = Intent()
intent.setClass(this@Wellcome, MainActivity::class.java)
startActivity(intent) 5.0 Then rewrite thread Inside run The method
myThread = object : Thread() {
override fun run() {
super.run()
handler.sendEmptyMessageDelayed(Constants.ZERO, 2000)
}6.0 Do the Activity Life cycle judgment is enough
override fun onResume() {
super.onResume()
if (!myThread.isAlive)
myThread.start()
}
override fun onStop() {
super.onStop()
// Determine whether the thread is still alive
if (myThread.isAlive) {
myThread.interrupt()
}
}I.e. implementation kotlin android Interface Activity Jump between ( The child thread delays two seconds to jump : In fact, it can not be used in the sub thread , Added for study )
Complete code
package com.nxm.muzi102.activity
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import com.nxm.muzi102.R
import com.nxm.muzi102.comment.Constants
class Wellcome : BaseActivity() { // Declare variables private lateinit var handler: Handler private lateinit var myThread: Thread override fun getContentView(): Int { return R.layout.activity_wellcome } override fun onViewsDidLoad(savedInstanceState: Bundle?) { initData() } fun initData() { // initialization myThread myThread = object : Thread() { override fun run() { super.run() handler.sendEmptyMessageDelayed(Constants.ZERO, 2000) } } // initialization handler handler = object : Handler() { override fun handleMessage(msg: Message?) { super.handleMessage(msg) if (msg?.what == Constants.ZERO) { var intent = Intent() intent.setClass(this@Wellcome, MainActivity::class.java) startActivity(intent) } } } } override fun onResume() { super.onResume() if (!myThread.isAlive) myThread.start() } override fun onStop() { super.onStop() // Determine whether the thread is still alive if (myThread.isAlive) { myThread.interrupt() } } }Jump to the page code
package com.nxm.muzi102.activity
import android.os.Bundle
import com.nxm.muzi102.R
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : BaseActivity() {
override fun getContentView(): Int {
return R.layout.activity_main
}
override fun onViewsDidLoad(savedInstanceState: Bundle?) {
main_tv_hello.text = "nxm"
}
}
Jump to the page layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.nxm.muzi102.activity.MainActivity">
<TextView android:id="@+id/main_tv_hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
边栏推荐
- 数字藏品如何赋能经济实体?
- What benefits have digital collections enabled the real industry to release?
- What is the magic of digital collections? Which reliable teams are currently developing
- Kotlin android简单Activity跳转、handler和thread简单配合使用
- visdom画多条动态损失曲线
- jvm-02.有序性保证
- 阿里云对象存储oss+picgo+typora实现构建图床
- [OWT] OWT client native P2P E2E test vs2017 build 6: modify script automatic generation vs Project
- jvm-06.垃圾回收器
- What does the English letter PC mean? What does the Internet PC mean
猜你喜欢

Wechat applet; AI intelligent dubbing assistant
![[OWT] OWT client native P2P E2E test vs2017 build 6: modify script automatic generation vs Project](/img/ce/2e0b0c0f6fae24b5b0df2680229284.png)
[OWT] OWT client native P2P E2E test vs2017 build 6: modify script automatic generation vs Project

What benefits have digital collections enabled the real industry to release?

visdom的使用

jvm-05.垃圾回收

Kotlin android简单Activity跳转、handler和thread简单配合使用

数字藏品如何赋能经济实体?
![[proteus simulation] Arduino uno+pcf8574+lcd1602+mpx4250 electronic scale](/img/2d/96a370c90dcb7091038afad33bc4b4.png)
[proteus simulation] Arduino uno+pcf8574+lcd1602+mpx4250 electronic scale

华为软硬件生态圈成型,从根子上改变美国对软硬件体系的领导地位

云原生数据库是未来
随机推荐
MySQL character set
Lottery DDD code
云原生数据库是未来
三项最高级认证,两项创新技术、两大优秀案例,阿里云亮相云原生产业大会
Wechat applet: an artifact for calculating the full amount of orders
HierarchyViewer工具找不到 HierarchyViewer位置
Oracle异常
Real MySQL interview questions (25) -- common group comparison scenarios
数字藏品如何赋能经济实体?
The author believes that the so-called industrial Internet is a process of deep integration of industry and the Internet
MySQL面试真题(二十一)——金融-贷款逾期
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.3 全双工, 节能, 自动协商机制, 802.1X 流控制 / 3.3.3 链路层流量控制
新课上线 | 每次 5 分钟,轻松玩转阿里云容器服务!
Centos7 installation of postgresql8.2.15 and creation of stored procedures
Data migration from dolphin scheduler 1.2.1 to dolphin scheduler 2.0.5 and data test records after migration
A bit of knowledge - folding forging and Damascus steel
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-9 personal solutions
ArcTime 制作中英文字幕视频
MySQL面试真题(三十)——贝壳-房产订单分析
PAT 乙等 1009 C语言