A man asked me how to use the callback, how to understand the callback?
How to say, only can be inexpressible, non-also, callback in the actual development of the use of frequency is actually very high, just as I was a child was also a callback function bullying, incredibly asked, then definitely want to share some of my experience.
comprehension of the net-pass 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 is not implemented in detail and requires class B to implement. After the class B implements the method. It is not going to invoke the method itself. Instead, it is passed to class A, for Class A to invoke. Such a mechanism is called a callback.
Expected to read already dizzy in the toilet, can temporarily ignore ...
Then from now on can first use my three elements to understand, the previous can be temporarily forgotten.
Callback My side summed up there are three elements:
- Interface:
哪里须要被监听 The interface is written there.
- Citation: Be able to have a reference talent enough
对被监听者进行设置监听 .
- Implementation:
哪里须要数据/结果 , where it is implemented
Examples of selling machines
A sale to the workers ordered 100 machines ready to sell to foreigners, let workers in the production of 100 machines after the notice him.
For this need we can use回调监听To handle
Let's start with a wisp of thought:
In this demand, we use just three elements to locate.
- Where to be monitored workers (monitor worker work progress)
- Where to get results--sales (results from workers ' production)
- Set up monitoring and sales for listeners (as the result is finally obtained in the sales class, so set the listener to be here)
Let's take a look at the following code:
Workers need to monitor the production machine in.
/** * Worker class * 1. Only be responsible for production * 2. Tell the manager when the production is finished * * * * * @author relicemxd * */ Public class Worker { PrivateWorklistner Listner;/** * Manufacturing Machines: * Worker-specific methods */ Public void Makemachine() {intnum = -; for(inti =0; i < num; i++) {if(Num >= -) {Continue; } }//Meet 100 machines to notify the next stepsListner.onworklistner (num); }/ * * Monitor worker Production Interface */ Public interface worklistner { //The method for event operation after the condition is met voidOnworklistner (intNUM); }/** * Set up a way to monitor workers ' production * @param Listner is implemented on demand side * * Public void Setonworklistner(Worklistner Listner) { This. Listner = Listner; }}
The above is to observe the work of the workers, so the WorkListner interface and setOnWorkListner(WorkListner listner) the setting of the monitoring method is written in the Worker worker class.
Continue looking at the following code:
The sales need to set up monitoring, only to know when the workers finished the machine.
/** * Sales are only responsible for selling can be * * * @author relicemxd * * * Public class Seller { Public Static void Main(string[] args) {//1. Sales to sell machines first, machines, who produce machines? Find WorkersWorker work =NewWorker ();//2. How many machines are sold for sale? Tell the WorkersWork.setonworklistner (NewWorklistner () {@Override Public void Onworklistner(intnum) {System.out.println ("Now the production is finished, total"+num +"Taiwan Machine");//When the workers are finished, they will be able to sell the machines.Sell (); } });//3. Workers know how many machines they will be able to dry. Let the workers produce 100 machinesWork.makemachine (); }/** * Selling machines: * Only sales can be the way * * Private Static void Sell() {System.out.println ("Sales started selling machines to foreigners ..."); }}
To know the progress of the machine, then there must be a new Worker() worker object, at this time with the worker's reference, and then what needs to command the workers (to do 100 machines) and tell the workers to finish giving feedback work.setOnWorkListner(listner) , the workers began to make the machine according to demand, work.makeMachine() after the completion of the feedback callback, The sale of the report can be sold.onWorkListner(int num) { TODO }
Suppose there's a doubt about it. Continue to see common callbacks in Android点击事件
The callbacks I've seen right now are callback methods that occur under a specific condition (such as the OnClick () of a button; the most common)
I first analyze the need to do the real thing is not the corresponding activity/fragment或者adapter , need to be monitored to see if there is a click on the status of whether or not Button .
Okay, I know. After the ins and outs, with three elements Su Tong under:
- Interface: Button
As you can see in the source code, the method of setting the listener is written in the Button class, and the OnClickListener Button parent class is written View .
publicvoidsetOnClickListener(@Nullable OnClickListener l) {? if (!isClickable()) {? setClickable(true);? }? getListenerInfo().mOnClickListener = l;?}
- Implementations and references
In the activity , when clicked button , I need to open a page MainActivity , so button the application and setOnClickListener(listner) methods are written in this activity .
button.setOnClickListener(new OnClickListener() {?@Override?publicvoidonClick(View v) {? mActivity.startActivity(new Intent(mActivity, MainActivity.class));?}});
Java callback function So to speak, it should be clear!