How to Delay Hits on Android Code
Have you ever received the requirement that you want this view to display this page for 2 seconds and then go to the new page or display items apart at a given time? How to do this? It takes a delay or delay. Yes, horse.
So how many ways is there to delay on Android?
Compare 3 easy-to-understand hit methods with code samples
.
1️⃣ Handler. PostDelayed.
The first method we've used since Java is super classic. 😆
Handler is a class of Android to help manage sending and processing messages on Thread queues. It works with Looper to receive and manage messages coming in loop.
Usually we create a Handler on Main Thread first and use postDelayed () to do some work after a certain period of time. This is to call doSomething () after a delay of 1000 milliseconds or 1 second.
Private val handler = Handler (Looper.getMainLooper ())
Handler.postDelayed ({doSomething ()}, 1000L)
The advantage is that it's easy to use and doesn't require any other libraries. It fits the task of running on Main Thread, but this one needs to be careful with memory leaks, because Handler still refers to the Activity or Fragment that we use to call Handler even after it's closed.
So don't forget to call the handler. removeCallbacksAndMessages (null) after use. I'll find that I'm not warning and the app is stale.
.
2️⃣ View. PostDelayed.
It is a delay using the View that we want to suit the delay associated with the View directly.
The principle works and looks like Handler.postDelayed (), but we don't have to build Handler ourselves, because Android has already done it for us.
For example, we have a view called myView. The requirement says that we do not show this view after two seconds.
MyView. PostDelayed ({
MyView. Visibility (View. GONE)
}, 2000L)
The precaution is that although it is convenient, if the View is removed from the screen, our code may not work, so we will use this method to ensure that the View is still present before the delay ends.
.
3️⃣ Coroutine and delay.
We're already Kotlin Android Developer, right? 😂
.
So it takes Coroutine. It can use lifecycleScope.launch with delay (), which makes our code look very organized and easy to read.
LifecycleScope. Launch {
delay (1000L)
Something ()
}
Advantages Of course, the code is easy to read. It has to deplay 1 second first and then do something (). And when you use the Lifecycle-bound lifecycle of Activity, it will cancel when Activity is destroyed. You don't have to create a handler and delete it again.
.
What methods do friends use? Or there are other postures, comment.
# Includes IT matters # IT should know # programmer # Developer # kotlin
สำหรับเพื่อนๆ ที่เริ่มเขียนแอป Android การเข้าใจการหน่วงเวลาเป็นเรื่องที่จำเป็นมาก ๆ เพื่อจัดการกับเหตุการณ์ต่าง ๆ เช่น การแสดงข้อมูลช้า ๆ หรือการหน่วงเปลี่ยนหน้าจอ ผมอยากเสริมเกี่ยวกับคำว่า Handler ที่หลายคนสงสัยว่าหมายถึงอะไรใน Android จริง ๆ แล้ว Handler คือคลาสที่ทำหน้าที่ในการส่งและจัดการข้อความหรือคำสั่งต่าง ๆ ให้ไปรันบน Thread ที่ต้องการ เช่น Main Thread หรือ Background Thread นั่นเอง ซึ่งข้อดีของ Handler.postDelayed คือ เหมาะกับการหน่วงเวลาแบบง่าย ๆ และไม่ต้องใช้ Library เพิ่ม นอกจากนี้ควรระวังเรื่อง memory leak เพราะ Handler อาจจะอ้างอิง Activity หรือ Fragment อยู่ ทำให้แอพค้างถ้าไม่ลบคำสั่งใน Handler เมื่อ Activity ถูกทำลาย ด้วยการเรียก handler.removeCallbacksAndMessages(null) หลังใช้งาน สำหรับ View.postDelayed นั้นสะดวกมากถ้าต้องการหน่วงเวลาที่เกี่ยวข้องกับ UI หรือ View โดยตรง เพราะ Android จะจัดการ Handler ให้เราเอง แต่อย่าใช้กับ View ที่อาจจะถูกลบออกจากหน้าจอในระหว่างหน่วงเวลา เพราะโค้ดอาจไม่ถูกเรียกใช้งาน สุดท้ายกับ Coroutine delay ซึ่งถ้าเราเขียน Kotlin อยู่แล้วถือเป็นวิธีที่สะดวกและทันสมัยมาก ๆ โดยใช้ lifecycleScope.launch ร่วมกับ delay() ทำให้โค้ดดูเรียบร้อยและเข้าใจง่าย อีกทั้งยังผูกกับ Lifecycle ของ Activity หรือ Fragment ช่วยลดปัญหาเรื่อง memory leak ได้มาก ภาพจากบทความที่แสดงตัวอย่างโค้ดและข้อควรระวังต่าง ๆ ช่วยให้มองเห็นภาพชัดเจน สำหรับใครที่ยังลังเลว่าจะใช้วิธีไหน แนะนำให้เลือกตามงานและสภาพแวดล้อมที่เหมาะสม เช่น ถ้าเป็น UI หน่วงเวลาง่าย ๆ ใช้ View.postDelayed, ถ้าต้องการความยืดหยุ่นใช้ Handler แต่ถ้าเขียน Kotlin แนะนำ Coroutine จะดีที่สุดครับ หวังว่าข้อมูลนี้จะช่วยให้เพื่อน ๆ สามารถนำไปใช้พัฒนาแอป Android ได้ง่ายขึ้นและปลอดภัยมากขึ้นครับ ถ้ามีเทคนิคอื่น ๆ หรือคำถามเพิ่มเติม ยินดีรับฟังในคอมเมนต์นะครับ!



