当前位置:网站首页>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>
边栏推荐
- PAT 乙等 1011 C语言
- Win11如何开启移动热点?Win11开启移动热点的方法
- node中操作mongoDB
- 制造业数字化转型存在问题及原因分析
- The difference between SaaS software and traditional software delivery mode
- Yingjixin ip6806 wireless charging scheme 5W Qi certified peripheral simplified 14 devices
- 编址和编址单位
- The author believes that the so-called industrial Internet is a process of deep integration of industry and the Internet
- AHA C language Chapter 7 you can do more with it (talks 27-28)
- Special research on Intelligent upgrading of heavy trucks in China in 2022
猜你喜欢

Real MySQL interview question (30) -- shell real estate order analysis

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

AI艺术的基因工程?使用 #Artbreeder 改变图像的任意形态

Use of visdom

技术开发团队视角看到的数字藏品机遇与挑战

Real MySQL interview questions (XXVI) -- didi 2020 written examination questions
![[graduation season u; advanced technology Er] farewell to the confused self in the past two years. Regroup, junior I'm coming](/img/04/3121514fcd8fcf1c939cbca7f4c67a.jpg)
[graduation season u; advanced technology Er] farewell to the confused self in the past two years. Regroup, junior I'm coming

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

Heimdall Database Proxy横向扩展提高20倍

Deploy docker and install MySQL in centos7
随机推荐
How to move the software downloaded from win11 app store to the desktop
PAT 乙等 1022 D进制的A+B
True MySQL interview question (XXII) -- condition screening and grouping screening after table connection
[proteus simulation] Arduino uno+pcf8574+lcd1602+mpx4250 electronic scale
runc 符号链接挂载与容器逃逸漏洞预警(CVE-2021-30465)
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.3 全双工, 节能, 自动协商机制, 802.1X 流控制 / 3.3.3 链路层流量控制
制造业数字化转型存在问题及原因分析
Wechat applet: elderly blessing short video
C primer plus學習筆記 —— 2、常量與格式化IO(輸入/輸出)
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-9 personal solutions
Software design and Development Notes 2: serial port debugging tool based on QT design
How can digital collections empower economic entities?
PAT 乙等 1011 C语言
Real MySQL interview questions (XXVII) -- Classification of users by RFM analysis method
数字藏品如何赋能经济实体?
C prime plus notes d'apprentissage - 2, constantes et formatage io (I / o)
jvm: 方法重载时,具体调用哪个方法,是由传入参数的静态类型来决定的,而不是由参数的实际类型来决定
MySQL面试真题(二十五)——常见的分组比较场景
Wechat applet; AI intelligent dubbing assistant
mysql字符集