What is the difference between View.postDelayed () and Handler.postDelayed () in the main thread?

According to the documentation of Handler.postDelayed(Runnable r, long delayMillis) :

Causes Runnable r to be added to the message queue, to run after a specified period of time. Startup in progress. in the thread to which this handler is attached.

On the other hand, View.postDelayed(Runnable action, long delayMillis) :

Causes Runnable to be added to the message queue that will be started after the specified amount of time has elapsed. The run will be launched on the user interface thread.

I want to know if there is a difference between them when they are called from the main thread and, in particular, if there is a difference when the action is destroyed?

I read an article on how I can flow in an Activity when I use an inner class handler, and I was wondering if View.postDelayed() could be used to lead to the same problem.

For example, can foo () cause a problem or kill an activity, decide that the anonymous Runnable class contains an activity reference?

 public class MyActiviy extends Activity { private void foo(View v) { v.postDelayed(new Runnable() { public void run() { // some delayed work } }, 60000); finish(); } } 
+6
source share
1 answer

From the source, View.postDelayed() just uses Handler.postDelayed() for the internal handler, so there is no difference.

foo() may skip activity, you should use View.removeCallbacks() to minimize this chance.

+8
source

Source: https://habr.com/ru/post/1014194/


All Articles