当前位置:网站首页>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>
边栏推荐
- 工作积累-判断GPS是否打开
- jvm-04.对象的内存布局
- ORB_ Slam2 operation
- True MySQL interview question (24) -- row column exchange
- TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.3 全双工, 节能, 自动协商机制, 802.1X 流控制 / 3.3.3 链路层流量控制
- 雷达图canvas
- 数字藏品火热背后需要强大的技术团队支持 北方技术团队
- Real MySQL interview question (23) -- pinduoduo ball game analysis
- 【owt】owt-client-native-p2p-e2e-test vs2017构建 6:修改脚本自动生成vs工程
- jvm-01.指令重排
猜你喜欢

数字藏品市场才刚刚开始

数字藏品如何赋能经济实体?

jvm-06.垃圾回收器

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

Redis cache penetration solution - bloom filter

编址和编址单位

高等数学(第七版)同济大学 习题1-8 个人解答

Raspberry pie assert preliminary exercise

What does the English letter PC mean? What does the Internet PC mean

HierarchyViewer工具找不到 HierarchyViewer位置
随机推荐
Wechat applet: elderly blessing short video
[Stanford Jiwang cs144 project] lab2: tcpreceiver
【owt】owt-client-native-p2p-e2e-test vs2017构建 6:修改脚本自动生成vs工程
Analysis on the problems and causes of digital transformation of manufacturing industry
Wechat applet: future wife query generator
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.3 全双工, 节能, 自动协商机制, 802.1X 流控制 / 3.3.3 链路层流量控制
AHA C language Chapter 8 game time is up (lesson 29)
华为软硬件生态圈成型,从根子上改变美国对软硬件体系的领导地位
PAT 乙等 1017 C语言
AHA C language Chapter 7 you can do more with it (talks 27-28)
PAT 乙等 1023 组个最小数
APP SHA1获取程序 百度地图 高德地图获取SHA1值的简单程序
Digital collections - new investment opportunities
PAT 乙等 1018 C语言
Heimdall database proxy scale out 20 times
Wechat applet: a new interesting test
iNFTnews | 加密之家从宇宙寄来的明信片,你会收到哪一张?
啊哈C语言 第7章 有了它你能做更多的事(第27-28讲)
jvm-05.垃圾回收
The author believes that the so-called industrial Internet is a process of deep integration of industry and the Internet