当前位置:网站首页>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

  1. 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 )

 Picture description here

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>
原网站

版权声明
本文为[Muzi 102]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230414189998.html