An in depth analysis of the CALLBACK mechanism of Android interface

  • 2020-11-03 22:35:12
  • OfStack

When using the callback interface found a often make the mistake, is likely to be of the inside of the callback function implementation using multi-threaded asynchronous task to do, or is it will cause we expect function callback is the result of the 1 to return to the main function, the actual found that doesn't work, because if you cannot and callback is multithreaded synchronization, the main function is the data returned is wrong, this is a very private one mistake. So what's a good way to do linear data transfer? Let me introduce the callback mechanism.

The callback function

A callback function is a function that is called via a function pointer. If you pass a pointer to a function as an argument to another function, it is said to be a callback function when the pointer is used to call the function to which it points. The callback function is not called directly by the implementer of the function, but is called by another 1 party when a particular event or condition occurs, and is used to respond to that event or condition.

Interface callbacks are often used in development.

The interface callback means that the execution does not occur immediately after registration, but is triggered at some point.

Here's an example:

One of A's questions was not possible. He went to ask B, but B could not solve it for the time being. B said, "I will tell you (A) after I (B) have solved it.

Only when B has solved the problem and tells A that the problem has been solved can A solve the problem.

Code such as the most commonly used:

1 Activity gives the button an interface callback method. Only when the user clicks the button and tells the button that it has been clicked will the button interface callback method be executed


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

Here is an Demo interface callback to understand:

When the asynchronous task receives the data, the data will be displayed with TextView

1. First, we need to define 1 interface, 1 method and 1 string as parameters:


 package com.xqx.InterfaceDemo;
 public interface ChangeTitle {
   void onChangeTitle(String title);
 }

2. Write an asynchronous task, take the interface as a constructor parameter, and determine if there is data in the doInBackground() method, then the interface calls back


package com.xqx.InterfaceDemo;
import android.content.Context;
import android.os.AsyncTask;
public class MyTask extends AsyncTask<String,Void,String>{
  private ChangeTitle changeTitle;
  public MyTask(ChangeTitle changeTitle) {
    this.changeTitle = changeTitle;
  }
  @Override
  protected String doInBackground(String... strings) {
    if (strings[0]!=null){
      changeTitle.onChangeTitle(strings[0]);
    }
    return null;
  }
}

3. Main Activity, pass this to 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 com.xqx.InterfaceDemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity implements ChangeTitle {
  private TextView textView;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (TextView) findViewById(R.id.textView);
    new MyTask(this).execute(" I am heading ");
  }
  //  Override the interface method to perform the corresponding operation 
  @Override
  public void onChangeTitle(String title) {
    textView.setText(title);
  }
}

Above content is this article to share the Android interface callback mechanism, thank you for your attention to the site, we will do better with your attention, thank you!


Related articles: