Callbacks in Android

Source: Internet
Author: User

1. Intro

The most classic callback in Android is the Click event Settings listener (typically via switch (V.getid ())). Here's the main one.

Btn_rigister.setonclicklistener (New View.onclicklistener () {    @Override public    void OnClick (view view) {        TODO log In    }});

View exposes an interface onclick

Public interface Onclicklistener {        /**         * Called if a view has been clicked.         *         * @param v The view that is clicked.         *        /void OnClick (View v);    }

When we use the implementation of the detailed method to write the processing, the current actvity is not to do any operation of this method, this enriched onclick method by the view callback processing!

One paragraph of the processing analogy

public Boolean Callonclick () {        listenerinfo li = mlistenerinfo;        if (Li! = null && Li.monclicklistener! = null) {            Li.mOnClickListener.onClick (this);            return true;        }        return false;    }
Detailed ability to read View.java


2. What is a callback

Callback is actually a two-way invocation pattern, it is said that the caller in the interface is called when the other side of the interface, it sounds very awkward, translation is "to implement the abstract class/interface instances of the implementation of the parent class provides an abstract method." After. The method is returned to the parent class for processing. The formula reads 3 times, can you understand the connotation of it?

The main thing to figure out is that the implementation method is returned to the parent class that provides the interface.

3. Why use Callbacks

This embodies the Java object-oriented "Everything is Object". We need to abstract the characteristics of ordinary objects. In general, there are characteristics, and each of the different characteristics need to be given to a particular situation to deal with, by exposing the interface method can reduce a lot of repetition. The code is more elegant.

For example, the view has a click of the continuity, but each click will produce a different event processing, so Android exposed an interface has an OnClick method. You have to deal with what you write,view just call this method , detailed how to deal with you have already done for the different click events.


4. How to Write

A very nice little chestnut

Define interface defines a interface in the class and defines an abstract method in the interface

Public interface callback{publicly          abstract void work ();        
【Interface Object"Defines a member variable for the interface in a class

Private Callback Mcallback;  
Set object defines a public method in the class that can set the object of the interface, calling the method to assign values to the interface object variable

public void Setcallback (Callback Callback) {          this.mcallback = Callback;      }  

"Invoke Method"invoking methods in an interface object

public void DoWork () {          mcallback.work ();      }  

Ok

Complete code such as the following

    public class Employee {          /          * * Defines the member variable of the callback interface */          private Callback mcallback;          /* Declaration Callback Interface          *          /public interface callback{public              abstract void work ();          }          /* * Set Callback Interface object member variable          */public          void Setcallback (Callback Callback) {              this.mcallback = Callback;          }          /* * Invoke method in callback interface object          *          /public void DoWork () {              mcallback.work ()          }      }  


And then we're going to use it for good.

    public class Boss {          private employee employee;          /* * Set the callback function for employee, where the detailed callback method is defined */public          void Setcallback () {              employee.setcallback (new Employee.callback () {                  @Override public                  void work () {                      System.out.println (' work ')}                  }              );}      }  





Callbacks 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.