Java callback function typical callback place in Android

Source: Internet
Author: User
Tags call back

A callback function in a computer is a reference to a piece of executable code passed through a function parameter to another code class, which is designed to allow the underlying code caller to define a subroutine at a higher level.

In Java we use interfaces to implement callbacks for functions.

The popular thing about callbacks is that programmers Cheung write a program (Callperson) with an interface (PhoneCall) with a callback function, and encapsulate the program (Callperson). Programmer Mumcall wrote the program to get

The Cheung Program (Callperson) calls the method in the program (Mumcall) of the write. The Cheung Program (Callperson) invokes the method of the program (Mumcall) through the interface (Callperson) of the program (PhoneCall).

Here are the following:

/*** Cheung Program Callperson *@authorJoker.chan **/ Public classCallperson {PrivatePhoneCall PhoneCall;  PublicCallperson (PhoneCall PhoneCall) {//TODO auto-generated Constructor stub         This. PhoneCall =PhoneCall; }         Public voidPhoneCall () {phonecall.phonecall (); }    }
 /**   * callback function Telephony interface *   @author   Joker.chan *  */ public  interface   PhoneCall { Span style= "color: #0000ff;"    >public  void   PhoneCall (); }
/**@author*/Publicclass implements  PhoneCall {    @Override    publicvoid  PhoneCall () {        // TODO auto-generated Method stub        SYSTEM.OUT.PRINTLN ("Mom called You Home for dinner!!!" );    }}

Call Method:

 Public Static void Main (string[] args) {        //  TODO auto-generated method stub        new  Mumcall ( );         New Callperson (MC);        Cp.phonecall ();    }

Operation Result:

Mom called to call you home for dinner!!!

Basic use of callback functions in Android.

1. Monitor the button

Button button = (Button)this. Findviewbyid (R.id.button); Button.setonclicklistener ( Newbutton.onclicklistener () {/// callback function @overridepublicvoidonClick (View v) { Buttontextview.settext ("button clicked");});

The above code adds an event listener to the button, which is actually one of the most common scenarios for "callbacks". We do not explicitly call the OnClick method ourselves. After the user triggers the button's Click event, it is automatically called by the Android system.

2. Life cycle functions

@OverridepublicvoidonCreate (bundlesaveinstancestate) {  super// you Code ... } @OverridepublicvoidonResume () {  super// you code ...}

The entire life cycle of the activity basically says that the callback function is functioning.

Advantages: The main advantage is to separate the parts of the code from the invariant, thus greatly improving the reusability and extensibility of the program.

The Android event listener is the interface of the view class and contains a separate callback method. These methods are called by the Android framework when a listener registered in the view is triggered by a user interface action. The callback method is included in the Android Event listener interface:

For example, the View object of Android contains a member variable named Onclicklistener interface, and the user's click action will be given to Onclicklistener's OnClick () method for processing.

If the developer needs to handle the Click event, you can define a Onclicklistener interface object to assign to the interface member variable onclicklistener of the view that needs to be clicked, typically with the view Setonclicklistener () function to complete this operation.

When a user clicks an event, the system will call back the OnClick () method of the Onclicklistener interface member that was clicked on view.

Instance (for the Emulation of button click event Listener on Android interface):

1. Define interface publicinterface  onclicklistener {       publicvoid  OnClick (Button b); }
2. Define buttonpublicclass  button {  Onclicklistener listener;     Public void Click () {    listener. OnClick (this);  }    Public void Setonclicklistener (Onclicklistener listener) {    this. Listener = listener;  }} 
3. To assign an interface object Onclicklistener to the interface member of a button Public classActivity { PublicActivity () {} Public Static voidMain (string[] args) {button button=NewButton (); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (Button b) {System.out.println ("Clicked");    }       }); Button.Click (); //User Click,system call Button.Click ();}

Java callback function typical callback place 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.