Brief discussion on Android Callback

one. Introduction, examples

      CallBack means callback. "Callback function" or "callback method" is a very important concept in software design and development. Mastering the idea of ​​"callback function" is very necessary for programmers (no matter which language is used) .

       What is a callback function? The callback function is a function reserved for system calls, and we know when the function is called. There are two points to note here: first, the callback function we write is not to be called by ourselves, but to be called by the system at some point in the future; second, we should know under what circumstances the system will call the callback function we write. callback function.

       To give an example of a "callback function" in real life, what is the first thing we do when answering questions in exams? That's right, write your school number and name. Note here that the student number and name we fill in are not for ourselves (that is, this method is not called for ourselves), but for the teacher to see when registering scores (reserved for future calls by the system). This is actually a callback. application.

Let's take a look at the scenarios where "callbacks" are applied in Android.

scene one:

 

<span style="font-family:Times New Roman;">Button button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(new Button.OnClickListener() {
  //回调函数  @override
  publicvoid onClick(View v) {
    buttonTextView.setText("按钮被点击了");
  }
});</span>

      The above code adds an event listener to the button, which is actually one of the most common application scenarios of "callback". We will not explicitly call the onClick method ourselves. After the user triggers the click event of the button, it will be automatically called by the Android system.

Scene two:

 

<span style="font-family:Times New Roman;">@Override
publicvoid onCreate(Bundle saveInstanceState) {
  super.onCreate(saveInstanceState);
  // You code...}
@Override
publicvoid onResume() {
  super.onResume();
  // You code...}</span>

      The above method is more familiar to everyone. This is the callback function set by the Android system in the Activity class. At different stages of the Activity life cycle, the Android system will automatically call the corresponding methods (onCreate, onPause, onResume, onDestroy, etc.)

  The above are two scenarios where "callback" is used in Android. Their code implementations may be different, but their ideas are similar, and they are all embodiments of the "callback" idea. Below, we simulate these two scenarios in Java.

First simulate registering an event listener. First write a listener interface

 

<span style="font-family:Times New Roman;">package com.listener;
/**
 * 点击监听器接口
 *
 */publicinterface MyOnClickListener {
    publicvoid onClick();
}</span>

Then write our own Button class

 

<span style="font-family:Times New Roman;">package com.listener;
publicclass MyButton {
    private MyOnClickListener listener;
    
    /**
     * 设置具体点击监听器
     * @param listener 点击监听器实现类
     */
    publicvoid setOnClickListener(MyOnClickListener listener) {
        this.listener = listener;
    }
    
    /**
     * 按钮被点击
     */
    publicvoid doClick() {
        listener.onClick();
    }
}</span>

Finally, simulate the client's registered listener and trigger the click operation.

 

<span style="font-family:Times New Roman;">package com.listener;
publicclass Client {
    publicstaticvoid main(String[] args) {
        MyButton button =new MyButton();
        //注册监听器        
        button.setOnClickListener(new MyOnClickListener() {

            @Override
            publicvoid onClick() {
                System.out.println("按钮被点击了");  
            }
            
        });
        //模拟用户点击        
       button.doClick();
    }
}</span>

The above is the application of the "callback" idea in event monitoring in Android. Let's simulate the second scenario, the embodiment of "callback" in the activity life cycle method call. Since it's relatively simple, I won't explain it too much. Let's just look at the code.

 

<span style="font-family:Times New Roman;">package com.activity;
public abstract class Activity {
    protectedvoid onCreate() {
        System.out.println("创建准备~~~~~~~");
    }
    
    protectedvoid onDestroy() {
        System.out.println("销毁准备~~~~~~~~");
    }
}</span>
<span style="font-family:Times New Roman;">
package com.activity;
publicclass ConcreteActivity extends Activity {
    @Override
    protectedvoid onCreate() {
        super.onCreate();
        System.out.println("创建中!!!");
    }
    
    @Override
    protectedvoid onDestroy() {
        super.onDestroy();
        System.out.println("销毁中!!!");
    }
}</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 

 

 

Guess you like

Origin blog.csdn.net/li1500742101/article/details/45198807