A Introduction, Example
Callback is the meaning of callback, "callback function" or "callback method" is a very important concept in software design and development, the idea of mastering "callback function" is very necessary for programmers (regardless of language).
What is a callback function? A callback function is a function reserved for a system call, and we know when the function is called. Here are two points to note: 1th, we write the callback function not to call itself, but to be ready for the system to be called at some point in the future, and 2nd, we should know under what circumstances the system will invoke our write callback function.
As an example of "callback function" in real life, what is the first thing we do when we test the answer? Yes, it's a school number and a name. Notice here, we fill in the number and name is not for ourselves to see (that the method is not for themselves to call), but to the teacher to register the score (pre-leave the system for future calls), which is actually a callback application.
Let's take a look at the scenarios that are applied to "callbacks" in Android.
Scenario One:
<span style= "Font-family:times New Roman;" >button button = (button) This.findviewbyid (R.id.button); Button.setonclicklistener (new Button.onclicklistener () { //callback function @override publicvoid OnClick (View v) { buttontextview.settext ("button clicked"); }}); </span>
The above code adds an event listener to the button, which is actually one of the most common scenarios for "callbacks". We do not show ourselves to call the OnClick method. After the user triggers the button's Click event, it is automatically called by the Android system.
Scenario Two:
<span style= "Font-family:times New Roman;" > @Overridepublicvoid onCreate (Bundle saveinstancestate) { super.oncreate (saveinstancestate); You code ...} @Overridepublicvoid Onresume () { super.onresume (); You code ...} </span>
The above method is more familiar, this is the Android system in the activity class set the callback function, at different stages of the activity life cycle, the Android system will automatically call the corresponding method (OnCreate, OnPause, Onresume, OnDestroy, etc.)
These are the two android in the "callback" scenario, their code implementation may be different, but the thinking is similar, is the "callback" thought of the embodiment. Below, we simulate each of these two scenarios in Java.
First, the registration event listener is simulated. Write a listener interface first
<span style= "Font-family:times New Roman;" >package com.listener;/** * Click Listener Interface * */publicinterface Myonclicklistener { publicvoid onClick ();} </span>
Then write a button class of our own
<span style= "Font-family:times New Roman;" >package com.listener;publicclass MyButton { private myonclicklistener listener; /** * Set Specific click Listener * @param listener tap Listener Implementation Class * /publicvoid Setonclicklistener (Myonclicklistener Listener) { This.listener = listener; } /** * button is clicked * /publicvoid DoClick () { listener.onclick (); }} </span>
Finally, the client's registration listener and trigger Click action are simulated.
<span style= "Font-family:times New Roman;" >package com.listener;publicclass Client { publicstaticvoid main (string[] args) { MyButton button =new MyButton (); Register Listener Button.setonclicklistener (new Myonclicklistener () { @Override publicvoid OnClick () { System.out.println ("button clicked"); } ); Simulate user click Button.doclick (); }} </span>
The above is the use of the "callback" idea in the Android event listener, we then simulate the second scenario, "callback" in the Activity life cycle method call embodiment. Because it is relatively simple, I do not explain more, we look directly at the code.
<span style= "Font-family:times New Roman;" >package Com.activity;public Abstract class Activity { protectedvoid onCreate () { System.out.println ("Create prepare ~~~~~~~"); } Protectedvoid OnDestroy () { System.out.println ("Destruction Readiness ~~~~~~~~");} } </span>
<span style= "Font-family:times New Roman;" >package Com.activity;publicclass Concreteactivity extends activity { @Override protectedvoid onCreate () { super.oncreate (); System.out.println ("Created in!!!" "); } @Override protectedvoid OnDestroy () { Super.ondestroy (); System.out.println ("Destruction in!!!" "); }} </span>
<span style= "Font-family:times New Roman;" >package com.activity;publicclass Client { publicstaticvoid main (string[] args) { activity activity =new Concreteactivity (); Activity.oncreate (); Activity.ondestroy (); }} </span>
Two Reference URL
1. http://xixinfei.iteye.com/blog/1306236
About Android Callback