Notes on the callback concept in Android

Source: Internet
Author: User

First, the callback function
A callback function is a function that is called through a function pointer. If you pass the pointer (address) of the function as an argument to another function, when the pointer is used to invoke the function it points to, we say this is a callback function. The callback function is not called directly by the implementing party of the function, but is invoked by another party when a particular event or condition occurs, and is used to respond to the event or condition.
Detailed Explanation:
The client program C calls a function A in the service program s, and then S, at some point, calls a function B in C, and for C, this b is called a callback function. For example, a window procedure function under Win32 is a typical callback function. Generally speaking, C does not call B,c itself to provide B's purpose is to let s to invoke it, and C has to provide. Since s does not know B name provided by C, S will contract B's interface specification (function prototype), and then by C advance through S's a function R tells S itself to use the B function, this process is called the callback function registration, R is called the registration function. Both the Web service and the Java RMI use a callback mechanism to access the remote server program.
Here's a popular example:
One day, I call to ask you questions, of course, is a problem, ^_^, you can not think of a solution, I can't hold the phone in there silly, so we agreed: Wait until you come up with a way to call me, so I hung off the phone to do other things. After xx minutes, my cell phone rang, you cheerfully said that the problem has been taken care of, should be treated as such. The story ends here. This example illustrates the programming pattern of asynchronous + callback. Which, you later on the phone to tell me the result is a "callback" process; my mobile phone number must be told you before, this is the registration callback function, my mobile phone number should be valid and the phone can receive your call, this is the callback function must conform to the interface specification.
The direct manipulation of pointers is not allowed in Java, so how does the callback be implemented?
Answer: It is implemented through an interface or an internal class.
Java method Callback is a method of function definition and function sharing, and it is a kind of coupling design idea. As an architecture, it must have its own operating environment and provide the user's implementation interface.
1. Define the interface Callback, including the callback Method Callback ()
2. Declare a callback interface object in a class caller Mcallback
3. The interface member (MCALLBACK) that is assigned to the caller object in the program is an inner class object such as
New Callback () {
Callback () {
The concrete implementation of the function
}
}
This allows the Mcallback interface member of the caller object to invoke the callback () method, when needed, to complete the callback:

Second, the use of callback mechanism in the Android framework
Here are a few examples:
1, in the activity defines a lot of life cycle of different states to invoke the method, these methods are empty implementation, the system framework to invoke, the user will also call to implement.
Instance (for the Emulation of button click event Listener on Android interface):
A Defining interfaces
Public interface Onclicklistener {
public void OnClick (Button b);
B. Defining a button
public class Button {
Onclicklistener Listener;
public void Click () {
Listener. OnClick (this);
}
public void Setonclicklistener (Onclicklistener listener) {
This.listener = listener;
}
}
C To assign an interface object Onclicklistener to the interface member of a button
public class Activity {
Public Activity () {
}
public static void Main (string[] args) {
Button button = New button ();
Button.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (Button b) {
System.out.println ("clicked");
}
});
Button.Click (); User Click,system call Button.Click ();
}
}
2, in the activity defines a lot of life cycle of different states to invoke the method, these methods are empty implementation, the system framework to invoke, the user will also call to implement.
Example (for simulation of activity on Android interface):
A. Defining interfaces
public interface activity{
public void onCreate ();
.....
public void ondestory ();
}
B. Implementation class myactivity of the Activity interface
Define a class to implement the Activity interface
Public CALSS MyActivity implements activity{
@Override//Implementation method, simple output
public void OnCreate () {
System.out.println ("Oncereate");
}
.....
@Override//Implementation method, simple output
public void Ondestory () {
System.out.println ("Ondestory");
}
}
C. System operating Environment Class Androidsystem
System Run Installation Class
public class androidsystem{
Defining constants

public static final int create=1;
....
public static final int destory=2;
How to Run
public void Run (Activity a,int state) {
Switch (state) {
Case CREATE:
A.oncreate;
Break
....
Case Destory:
A.ondestory ();
Break
}
}
}
D. Test class
Test class
PUBLILC class test{
public static void Main (string[] args) {
Instantiate Androidsystem
Androidsystem system = new Androidsystem ();
Instantiate myactivity
Activity a = new myactivity ();
System.run (a,androidsystem.creaate);
....
System.run (a,androidsystem.destory);
}
}
As can be seen above, the interface (System framework) is provided by the system, the implementation of the interface is user-implemented, so that the interface can be unified to achieve different effects.
System in different states "callback" our implementation class to achieve the classification of interfaces and implementations.

Notes on the callback concept in Android

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.