SoFunction
Updated on 2025-04-04

An in-depth analysis of the callback mechanism of Android interface

When using interface callbacks, I found a common mistake, that is, the implementation of the callback function may be done with multi-threaded or asynchronous tasks, which will cause us to expect the function callback to return the result of a main function. In fact, it is not feasible, because if the callback is multi-threaded, you cannot synchronize with the main function, that is, the data returned is wrong, which is a very secret error. So is there any good way to achieve linear data transmission? Let’s first introduce the principle of the callback mechanism.

Callback function

A callback function is a function called through a function pointer. If you pass the pointer (address) of the function as an argument to another function, when this pointer is used to call the function it points to, we say this is a callback function. The callback function is not called directly by the implementer of the function, but is called by another party when a specific event or condition occurs, and is used to respond to the event or condition.

During development, interface callbacks are often used.

The meaning of an interface callback is that it is not executed immediately after registration, but triggers execution at a certain time.

For example:

A has a problem and he doesn't know how to solve it. B said, "I will tell you when I (B) solve it. At this time, A can continue to do other things first."

Then A can solve this problem only when B solves the problem and tells A to solve the problem.

For example, the most commonly used code:

An Activity gives the button an interface callback method. Only when the user clicks this button and tells the button that the button is clicked will the button interface callback method be executed.

Button btn = new Button(this);
    (new () {
      @Override
      public void onClick(View view) {
        
      }
    });

Then, the following is a demo to understand the interface callback:

The main thread starts an asynchronous task. When the asynchronous task receives data, the data will be displayed using TextView.

1. First, we need to define an interface, a method, and the parameters are a string:

 package ;
 public interface ChangeTitle {
   void onChangeTitle(String title);
 }

2. Write an asynchronous task, use the interface as a constructor parameter, and judge in the doInBackground() method if there is data, the interface callback will be made.

package ;
import ;
import ;
public class MyTask extends AsyncTask<String,Void,String>{
  private ChangeTitle changeTitle;
  public MyTask(ChangeTitle changeTitle) {
     = changeTitle;
  }
  @Override
  protected String doInBackground(String... strings) {
    if (strings[0]!=null){
      (strings[0]);
    }
    return null;
  }
}

3. The main activity, pass this to the asynchronous task parameters, that is, the interface callback method is executed in this class, so it is necessary to implement the ChangeTitle interface and rewrite the interface.

onChangeTitle method

package ;
import ;
import ;
import ;
import ;
public class MainActivity extends Activity implements ChangeTitle {
  private TextView textView;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView();
    textView = (TextView) findViewById();
    new MyTask(this).execute("I'm the title");
  }
// Rewrite the interface method and perform the corresponding operation  @Override
  public void onChangeTitle(String title) {
    (title);
  }
}

The above content is the Android interface callback mechanism shared by this article. Thank you for your attention to my website. With your attention, we will do better. Thank you!