Kotlin entry (29) task runnable

Source: Internet
Author: User

The task runnable defines a code snippet that can be run independently. It is usually used for interface control delay processing. For example, to avoid conflicts between resources simultaneously occupied, sometimes it is to refresh the interface repeatedly to produce the animation effect. You can run a task in multiple forms. You can call the post or postdelayed method of the processor object in the UI thread, and enable the sub-thread to execute the runnable object. Before running a task, you must declare the object of the task before the caller can execute the task. The kotlin Code declares the runnable object in four ways, which correspond to different business scenarios respectively. Next, we will explain the four declaration methods of the runnable object in sequence:

First: internal class
The internal class method is the most regular. In the code, first write an internal class inherited from runnable, then rewrite its run method, and fill in the specific business logic processing. Taking the most common counter as an example, the counter result after adding one to the interface is displayed every second. If the internal class method is used for change, the following kotlin code is used:

Private Val handler = handler () Private var COUNT = 0 inner private class counter: runnable {override Fun Run () {count ++ TV _result.text = "the current Count value is: $ count "handler. postdelayed (this, 1000 )}}

 

In the oncreate method of the activity page, add a line of code up and down, and run the following command to execute the Count task:

    handler.post(Counter())

Second: anonymous internal class

The internal class method is the most formal and undoubtedly the most wordy. Because this counting task is only started when the page is opened, you do not need to explicitly construct it. You only need to declare the task instance when defining the internal class. In this case, the declared code changes from the internal class method to the anonymous internal class method. If kotlin encoding is adopted, pay attention to the use of the keyword object placeholder, indicating that this is an anonymous internal class. The complete kotlin code is as follows:

Private Val counter = Object: runnable {override Fun Run () {count ++ TV _result.text = "the current Count value is: $ count" handler. postdelayed (this, 1000 )}}

 

Because the task instance is declared when the internal class is defined, the processor can directly run the instance to start the task:

    handler.post(counter)

 

Both the internal class and anonymous internal class have the complete form of the class. Therefore, their run method allows the keyword "this" to refer to this handler class. In the example code, "handler. postdelayed (this, 1000) "means to repeat your tasks after one second. Because the task can be repeated, these two methods can be used to continuously refresh the animation effect of the interface.

Third: anonymous Functions
The preceding two internal class implementation methods have the complete form of classes, which means that the run method must be explicitly rewritten. However, this task class must only rewrite the run method, even if developers do not write it out, the run method cannot be escaped. As early as the first chapter, in order to demonstrate the indirect nature of the kotlin code, we gave an example of "button object. setonclicklistener {code for processing click events} ". This method uses the Lamba expression to omit onclick, the only method of the Click Event interface. Therefore, the task object in this section can also be written in a similar way. As long as the object is of the runnable type, the redundant run method can be removed as expected. The kotlin code after the task object is rewritten is as follows:

Private Val counter = runnable {count ++ TV _result.text = "the current Count value is: $ count "}

 

Obviously, the counter is still runnable, so the processor can start the task after running the instance:

    handler.post(counter)

 

However, this method removes the run method at a cost. Although the Code becomes concise on the surface, it does not have the complete structure of the class. The internal this keyword does not represent the task class itself, the host class is the activity class. In view of this change, the post or postdelayed method of the processor cannot be called internally, meaning that the task instance cannot call itself repeatedly. Therefore, a task object using an anonymous function is applicable when refresh is not required.

Type 4: anonymous instances
Note that the previous counter is a task object assigned by equal signs. In this case, it is better to directly Insert the expression on the right of the equal sign into the POST method, as shown in the following kotlin code:

//: Enter the definition code handler. Post of the runnable object in the POST method (runnable {count ++ TV _result.text = "Current Count value: $ count "})

 

The above code can be further simplified, because the POST method can only input runnable parameters, so the runnable inside the brackets is redundant. In addition, the POST method has only one input parameter, therefore, the nested braces in parentheses are a little complicated. Delete and merge the two redundancy, respectively, and get the kotlin code of the anonymous instance version:

//: If the task only needs to be executed once, you can embed the Execution Code handler of the task in the form of an anonymous instance. post {count ++ TV _result.text = "the current Count value is: $ count "}

 

The above method of removing parentheses is only suitable for calling the POST method with only one parameter. If there are multiple input parameters in other methods, such as the postdelayed method, the parentheses in the outer layer still need to be retained, in this case, braces and their internal code are passed in as a function parameter. The calling code of kotlin with parentheses restored is as follows:

//: If the task is delayed, the anonymous instance can be used as the input parameter handler of postdelayed. postdelayed ({count ++ TV _result.text = "the current Count value is: $ count"}, 1000)

 

The anonymous instance method directly writes the task code in the call function, which means that the task Code cannot be called elsewhere. Therefore, the application scenario of this task is narrower. Although anonymous functions cannot be called repeatedly, they are still allowed to be called multiple times in different places. However, an anonymous instance can only Flash in the Flash where it is waiting, therefore, you must select a proper task method based on actual business requirements.

Kotlin entry (29) task runnable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.