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

Source: Internet
Author: User

When using the interface callback, we found a common mistake, that is, the implementation of the callback function may be done with multithreading or asynchronous tasks, which leads us to expect the function callback completed to return the results of a main function, the actual discovery is not feasible, Because if the callback is multi-threaded, you can't sync with the main function, that is, the returned data is wrong, which is a very secret error. So what's a good way to achieve the linear transmission of data? The principle of the next callback mechanism is introduced first.

callback function

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

In development, interface callbacks are often used by us.

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

As an example:

A there's a problem. No, he went to ask B,b for the time being, B said, and I'll let you know when I'm done. (a) At this point A can continue to do something else first.

Then it is only when B solves the problem by telling a that the problem is solved, that a can solve the problem.

In code, such as the most commonly used:

An activity to give the button an interface callback method, only the user clicked the button, told the button was clicked, will perform the button interface callback method

Button btn = New button (this);
    Btn.setonclicklistener (New View.onclicklistener () {
      @Override public
      void OnClick (view view) {
        
      }
    });

So here's a demo to understand the interface callback:

The main thread opens an asynchronous task that, when the asynchronous task receives the data, displays the data in TextView.

1, first we need to define an interface, define a method, the parameter is a string:

 Package Com.xqx.InterfaceDemo;
 Public interface Changetitle {
   void Onchangetitle (String title);
 }

2, write an asynchronous task, the interface as a construction method parameters, in the Doinbackground () method to determine if there is data, the interface callback

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, the main activity, to the asynchronous task parameters pass this, that is, the interface callback method in this class, you need to implement the Changetitle interface, rewrite the port

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 the title")
  ;
Overrides the interface method, performing the appropriate action
  @Override public
  void Onchangetitle (String title) {
    Textview.settext (title);
  }

The above content is this article gives everybody to share the Android interface callback mechanism, thanks everybody to cloud Habitat community website's attention, has your attention we can do better, thanks!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.