Notes about callback in Android

Source: Internet
Author: User

Notes about callback in Android
I. Callback Functions
A callback function is a function called by a function pointer. If you pass the pointer (address) of a function as a parameter to another function, when this pointer is used to call the function to which it points, we will say this is a callback function. The callback function is called by another party when a specific event or condition occurs instead of by the implementer of the function. It is used to respond to the event or condition.
Explanation:
Client program C calls A function A in service program S, and then S calls A function B in C at some time. For C, this B is called A callback function. For example, the window procedure function under Win32 is a typical callback function. Generally, C does not call B by itself. C provides B for S to call it, and C has to provide it. Because S does not know who the B surname provided by C is, S will agree with the interface specification (function prototype) of B ), then C tells S to use the B function through a function R of S in advance. This process is called the registration of the callback function, and R is called the registration function. The callback mechanism is used by Web Service and Java RMI to access remote server programs.
The following is a common example:
One day, I called to ask you a question. Of course it was a problem. ^ _ ^. You couldn't figure out a solution at the moment, and I couldn't wait there with a phone. So we agreed: after you have come up with a solution, you can call me to notify me. In this way, I will stop calling to handle other things. After XX minutes, my cell phone rang, and you said with high enthusiasm that the problem had been fixed, so you should handle it like this. The story ends here. This example illustrates the "Asynchronous + callback" programming mode. When you call a mobile phone to tell me that the result is a "Callback" process. My mobile phone number must be told before. This is the registration callback function; my mobile phone number should be valid and the mobile phone can receive your call. This is the callback function that must comply with the interface specifications.
JAVA does not allow direct pointer operations. How is its callback implemented?
Answer: It is implemented through interfaces or internal classes.
JAVA method callback is a means to define and share functions, and is a coupling design concept. As an architecture, you must have your own runtime environment and provide your implementation interfaces.
1. Define the interface Callback, including the callback method Callback ()
2. Declare a Callback interface object mCallback in a class Caller
3. Assign the Caller object interface member (mCallback) An internal class object in the program, such
New Callback (){
Callback (){
// Function implementation
}
}
In this way, the callback () method can be called by the mCallback interface member of the Caller object as needed to complete the callback ..

Ii. Use of callback mechanism in Android framework
Here are several examples:
1. Define many methods to be called in different states of the life cycle in the Activity. These methods are empty implementations. the system framework must be called and the user must also call them.
Instance (simulate a Button click event listener on the Android Interface ):
A. Define the interface
Public interface OnClickListener {
Public void OnClick (Button B );
B. Define the Button
Public class Button {
OnClickListener listener;
Public void click (){
Listener. OnClick (this );
}
Public void setOnClickListener (OnClickListener listener ){
This. listener = listener;
}
}
C. Assign the OnClickListener interface object to the interface member of the 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. Define many methods to be called in different states of the lifecycle in the Activity. These methods are empty implementations. the system framework must be called and users must also call them.
Instance (simulate the Activity on the Android Interface ):
A. Define the interface
Public interface Activity {
Public void onCreate ();
.....
Public void onDestory ();
}
B. Implementation class of the Activity interface MyActivity
// 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. AndroidSystem
// System running and Installation
Public class AndroidSystem {
// Define a constant

Public static final int CREATE = 1;
....
Public static final int DESTORY = 2;
// Running Method
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, and the implementation of the interface is implemented by the user, so that the interface can be unified to achieve different effects.
The system calls back our implementation classes in different States to achieve classification of interfaces and implementations.

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.