Android's callback event detailed _android

Source: Internet
Author: User

It's complicated to see some of the corrections on the web, especially based on Android's custom callbacks, feeling confused, and I wrote this one based on my explanation of the callbacks.

Let's look at a simple example:
There are two classes of ClassA, and CLASSB, ClassA invoke the method inside the CLASSB,

public class ClassB {public

  void Method_from_classb () {for
    
    (int i=0;i<10;i++)
      System.out.print ("..." + i);
  }
public class ClassA {public

   
  
  static void Main (String args[]) {
   
    CLASSB classb = new ClassB ();
    
    CLASSB.METHOD_FROM_CLASSB ();
  }


Output:

... 0...1...2...3...4...5...6...7...8...9

The groove, which silly write blog, insulting my IQ is not, Hee, is to do comparisons, then look at the use of callback, ClassA is how to call the method in ClassB, note that the callback:

Let CLASSB implement the interface defined by ClassA

public class CLASSB implements classa.classainterface{public

  ClassB () {
    
    new ClassA (). RegisterInterface (this);
    System.out.println (".....) ClassB ... "+this);
  }

  @Override public
  void Method_from_interface () {for

    (int i=0;i<10;i++)
      System.out.print ("..." +i);
  } /
  
  * Public  void Method_from_classb () {for
  
  (int i=0;i<10;i++)
    System.out.print ("..." +i);
  } */
}

ClassA defines interfaces and abstract methods:

public class ClassA {public

  static classainterface Classainterface;
  
  Public interface classainterface{public
    
    void Method_from_interface ();
  }
  public void RegisterInterface (Classainterface a_interface) {

    this.classainterface = a_interface;
    System.out.println ("... a_interface" +a_interface);
  }
 
  public static void Main (String args[]) {
   
    CLASSB classb = new ClassB ();//Tag @1, explain later
    //classb.method_from_ CLASSB ();
    System.out.println ("... classainterface ..." +classainterface);
     if (classainterface!= null) {
        classainterface.method_from_interface ();}}}


Output:

... 0...1...2...3...4...5...6...7...8...9

Sorted out, that is, I defined an interface (interface) inside the ClassA, which defines a method in the interface, but does not do anything without a method body.

When ClassA executes to the Mian () function, the method of the interface is invoked, but it is said that the interface method does not implement the specific thing, it will find the corresponding method within the CLASSB to achieve the specific things.

Yo Tu, ClassA interface method is how to find the ClassB method, it will be heaven???

The following analysis of how this code is God:

Using the callback of the interface to implement the method in ClassB

 classAInterface.method_from_interface();

I printed the log for analysis in the above code using SYSTEM.OUT.PRINTLN:

First (method in ClassA):

  public void RegisterInterface (Classainterface a_interface) {

    this.classainterface = a_interface;
    System.out.println ("... a_interface" +a_interface);
  }

Output:

... a_interface ... classb@3ddb8962

The second one:

Public ClassB () {
    
    new ClassA (). RegisterInterface (this);
    System.out.println (".....) ClassB ... "+this);
  }

Output:

... ClassB ... classb@3ddb8962

The third one:

System.out.println ("... classainterface ..." +classainterface);
     if (classainterface!= null) {
        classainterface.method_from_interface ();
      }

Output:

... classainterface ... classb@3ddb8962

See here is not the epiphany, the output is "classb@3ddb8962" that is the CLASSB object reference!!!

Ah! The interface is simply a reference to the ClassB object to the ClassA, then this sentence will be the Heaven's statement is not very good explanation.

Classainterface.method_from_interface ();

Equivalent to Classb@3ddb8962.method_from_interface ();

This is not with the top to the code:

    CLASSB CLASSB = new ClassB ();  
    CLASSB.METHOD_FROM_CLASSB ();

Again, that's why I started with this example!!!

I believe it's a matter of understanding what the interface callback is going to look at here.
But there is a bit of confusion, why to interface callback so troublesome, the top of the ClassA inside execution:

    CLASSB CLASSB = new ClassB ();  
    CLASSB.METHOD_FROM_CLASSB ();

Not so can ClassA call ClassB inside the method .... But if ClassA to call CLASSC,CLASSD ..., the inside of the method, is not also to change the ClassA inside the code, instantiate CLASSC,CLASSD ... Object, obviously is not good, if uses the interface that does not need to change ClassA inside the code, any class as long as realizes ClassA inside the interface to be possible.

Explain the tag @1:

The above phrase seems to violate the Mark @1, and in ClassA it is really necessary to instantiate CLASSB objects.

Because the code in the construction method is executed to "take advantage" of initialization:

Public ClassB () {
    //is equivalent to the registration of callback events, the beginner's callback null pointer is likely to forget the ' register ' of the
    New ClassA (). RegisterInterface (this);
  }

This is passed to ClassA, which is the function of using log analysis above.

But there's no need to save you in Android development.
Can be performed at the time of initialization of an activity:

@Override
  protected void onCreate (Bundle savedinstancestate) {

     //is equivalent to the registration of callback events, the beginner's callback null pointer is likely to forget ' register ' here.
    New ClassA (). RegisterInterface (this);
}

In Android development, the Mian () function in ClassA can be replaced with events, triggering:
Such as:

Button.setonclicklistener (New Onclicklistener () {
      
      @Override public
      void OnClick (View v) {
        //TODO The automatically generated method stub
        if (Classainterface!= null) {
        classainterface.method_from_interface ();
      }
    });

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.