function calls & callback functions in Java/android & Custom callback Functions

Source: Internet
Author: User

When it comes to customizing callback functions when you're doing Android custom controls, think about the technology you're not looking for, and go back to the question of callback functions in Java again. Unfortunately, too many cluttered posts and blogs on the web are all spinning around, and that's the "C callback function ...." Pointers ..... java .... ", it is not a point of view of their own ideas, it is estimated that which is copied! (hehe, either is spit groove right, or is my level is too bad to read not to understand also to add comments) There are some very good articles, I will add a link in the final reference, we can see.

So let's get started on our topic-what is a callback function?

Let's start with a function call, one step at a depth:

What is a function (method) call?

(All the other languages have forgotten ...) Oh, just Java) the function call in Java is nothing more than (1) A method in a class in order to complete a business in the execution of the call to another method, another method can be itself, that is recursive, (2) different classes of function calls, such as class B (caller caller) a method to invoke the Class A object (Callee callee) (whether a is a member of B or passed in as a function parameter). method calls are essential to our programming, and we may not be able to turn a blind eye to it, otherwise, how does a different class of engineering work together? Although simple, or draw a picture of it, will be compared to better explain the callback mechanism:

A function such as Class B method_b to invoke the Method_a1 method of member A (Class A) during execution.

That's what we did when we first started learning. But there's a problem with that, so let's review what we learned in the past and constantly explain the problem: for example, otherwise clumsy is not clear--a class is a bird, B is our tool class, now we need to do this in the tool class: Through the method in the tool class Method_ b Complete the correct movements of the different birds flying.

The first stage : Obviously the two classes can not complete this requirement! Then we learned the inheritance, we created the "Sparrow", "Wild Goose", "ostrich" ... A variety of "bird" subclasses, through different subclasses to complete the different fly method, and then in the tool class to create a different subclass of the object assigned to a, so the requirements are complete. But looking back, we created a whole bunch of subclasses, and also used implicit type conversion (subclass assignment to parent class), obviously not satisfied;

Second Stage : Later, we learned the overloaded function, in the birds to create a series of the same name of the "Fly" function, the parameters of the type is Sparrow, wild goose ..., so that we do not have to implicitly convert, directly call Bird Object A's fly method, into the different geese, sparrows ..., A will be able to fly correctly. This requirement is also completed, although the use of implicit conversions is avoided, but the large number of subclasses may not fall. This obviously does not meet our expectations (what is our expectation?). is to do the least work, the bird can fly correctly).

the problem with this approach is obvious: on the one hand, we have to maintain the numerous subclasses, add a lot of work, on the other hand, this kind of coding lacks flexibility, how many kinds of birds do you know, how each bird flies, you know, if several birds are very similar, do you create different classes or belong to a class? so with this implementation, the "callback function" is used--

What is a callback function?

Go straight:

For example, the interface must be used in a callback function. The following is a sense of well-written understanding:

The word "callback" is often heard or seen in the course of Android learning, so what is a callback? The so-called callback function is: Defines a method in Class A, this method uses an interface and an abstract method in the interface, but the abstract method does not have a concrete implementation, requires class B to implement, B class implementation of the method, it does not call the method itself, but passed to class A, for Class A to call, This mechanism is called a callback.

I think that the method in class B method_b when invoking the method_a1 of the A object, method_a1 execution to interface's abstract method does not know how to implement, just B in call him when the concrete implementation, so method_a1 return to call method _b provides the implementation, which is the callback. In fact, the callback function is to use interface or its abstract method to leave a hole in the method of an indeterminate implementation, leaving the specific caller (calling the indeterminate method in front of it) to provide a concrete implementation at the time of invocation to fill that hole. This allows for more flexible coding purposes and greatly reduces the use of subclasses. just take the above example and continue

We do this to achieve: first define an interface, the interface declaration abstract method "Fly", in the "Bird" class of "take off" method of the interface object as a parameter, the rest of what to do, where to fly to do not know how to fly on the call interface provides the abstract method "Fly" In the tool class, call the "take off" method of the "Bird" class to implement the abstract method of the object as a parameter passed in, and then you want it to fly how to fly, the concrete implementation is you call the time is written. Well, how about this, huh? Without the implicit conversion, not a large number of sub-classes, when the call encountered what birds fly, to achieve our goal of less work!

What is a custom callback function?

The custom callback function, as the name implies, is our own definition of the callback function. In fact, the above example is a custom callback function! We're used to calling callback functions defined by others called callback functions,and the onclick event response of TextView, ImageView, and their subclass controls in the Android system is a typical callback mechanism . About this prawn is better than me--detailed introduction to the callback function mechanism in Android, detailed look will be helpful!

An example of a simple custom callback function

Finally, for a simple example with code, take a look at the operation of the callback function:

First, we define a interface:

[Java]View PlainCopy
    1. Public interface MyInterface {
    2. void Sayyourname ();
    3. }


Next, we define a class, where one method takes an object of the interface MyInterface type as a parameter:

[Java]View PlainCopy
  1. Public class MyClass {
  2. Public MyClass () {
  3. LOG.E ("Wangj", "Myclass-constructor"); //Callout constructor
  4. }
  5. / * Use an object of the interface type as the input parameter * /
  6. public void Sayyourname (MyInterface myinterface) {
  7. LOG.E ("Wangj", "Myclass-sayyourname_start"); //Labeling method start
  8. Myinterface.sayyourname (); //Encounter an abstract method that uses an interface when you do not know the specific implementation
  9. LOG.E ("Wangj", "Myclass-sayyourname_finish"); //Method end
  10. }
  11. }


Finally, we call this class in the activity, create the object and invoke its method, and implement the concrete implementation logic of the abstract method in the interface for the callback to use:

[Java]View PlainCopy
  1. Public class Mainactivity extends Activity {
  2. @Override
  3. protected void OnCreate (Bundle savedinstancestate) {
  4. super.oncreate (savedinstancestate);
  5. Setcontentview (R.layout.activity_main);
  6. MyClass MyClass = new MyClass ();
  7. Myclass.sayyourname (new MyInterface () { //implements the interface and passes in as a parameter
  8. @Override
  9. public void Sayyourname () {
  10. LOG.E ("Wangj", "Callback-interface-implementor"); //specific operation implementation
  11. }
  12. });
  13. }
  14. }


Well, run it (we don't have any interface for this example, the default activity interface, look at the log):

The order of operation is what we understood before: A method in B is called, the method in a is returned to B when it is run to an abstract method in the interface to look for a specific implementation (this is the callback), and the callback completes after the completion of the incomplete steps below.

Well, that's the callback function I've known, and it sounds profound, and it's not too difficult to find out when you know it. But think about how visionary Java developers are at designing this mechanism (good worship, though not knowing who he is)! Writing is limited, understanding is not enough, if there are deficiencies or mistakes, please correct me! Finally, I enclose some good articles--

function calls & callback functions in Java/android & Custom callback Functions

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.