Understanding of callback functions in Android --- I am a beginner in Android and a callback function in android

Source: Internet
Author: User

Understanding of callback functions in Android --- I am a beginner in Android and a callback function in android

I am a sophomore from the National Congress. I just got in touch with Android and the cnblog blog I just applied for. I 'd like to explain my understanding of the callback function in Android, in Android, the callback function is similar to the default constructor in C ++ and JAVA, that is, the code automatically called when a certain amount of code is run, the difference between the callback function in Android and the default constructor in C ++ and JAVA is that the default constructor in C ++ and JAVA is automatically called when an object is created, the callback function in Android is automatically called after, for example, pressing the HOME Key.


How does a callback function in java understand that the callback function in android is different from that in java?

It can be understood as an extension of logic. For example, Methods Starting with onXXX in java.
These methods are usually an extension of information. It indicates that some events have occurred. This information is usually part of some big logic. Other parts do not need to be considered by developers.
For example, if you understand the onCreate method, you will know that it is the first code to be executed in the whole activity. Then who calls it. This generally does not need to be considered. You only need to know that the system is calling. This method is executed. You need to supplement the implementation of this method. Therefore, you have completed the rest of the event.

For example, you need to get up after an alarm in the morning. You can set a timer or a thread to run the timer. When it is found. You need to throw this event. Who will handle it. This is not the scope of the alarm. Its function is to trigger the event. As for processing. You need to handle this event. You can use many methods to create associated handles.
For example. Through the implementation of the interface. Inheritance. Or broadcast in android.

The callback function describes a mode. This is irrelevant to the language or platform. Therefore, android and java are the same. C ++ is also available.

Android explicitly calls the callback function

First, obtain the button id, for example
Button btn = (Button) findViewById (R. id. btn1 );
Btn. setOnClickListener (new OnClickListener (){
Public void onClick (View v ){
// TODO Auto-generated method stub

}
});

Related Article

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.