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
}
});