Correct understanding and use of callback methods

The callback method is an easy-to-understand method, but many people mixed in various strange concepts when explaining it, which made the callback method very unclear. Now I will explain the callback method to you. If I am wrong, welcome to point out.
My main function
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glSurfaceView = new MyGLSurfaceView();
}
I created this glSurface object in the main function. As for what this object does and what to pass, no one needs it Concerned, in short, I just created an object in the main function, and then passed some parameters into it.
Then this glSurfaceView class is performing various complex operations.
When this object performs complex operations inside it, my main function cannot see or control it. At this time, we want to pass data to our main function from a certain node in glSurfaceViews, or do something, which is completely impossible. So if we want to control, we can only use
Mainactivity mactivity;
glSurfaceView = new MyGLSurfaceView(mactivity); then write Mainactivity glSurfaceViewactivity=mactivity
in the construction method of glSurfaceView ; then write glSurfaceView .




Every time we create glSurfaceView, will we create an activity in it, then you have to use this activity in it and then destroy it. Otherwise, a large number of activity instances will be created.
If there are a lot of resources in the activity, there is no doubt that your project will be stuck soon
and some complex programs, we can't see anything in this glSurfaceView at all, maybe it is completely blocked. Packaged. At this time, when we want it to do something, it is simply impossible to notify our activity.
At this time, if I still want to achieve the effect just now, I have to introduce a callback function.
What does the callback function do? In fact, it is very simple. When I do something in the glSurfaceView function, I give it a mark. Whenever this mark is executed, other functions can do other things. You can understand it as a notification to notify other functions that my work is done, or I have done this thing, now Come do your thing.

 public interface Callback{
        void start();
        void stop();
    }

    private Callback mCallback;
    public void setCallback(Callback callback){
        mCallback = callback;
    }

First define an interface in glSurfacaView, and write the place where you want to set the callback,
such as
@Override
public void onPrepared(MediaPlayer mp) {

       if(mCallback!=null){
            mCallback.start();

        }
    }

public void stop() {

        if(mCallback != null){
            mCallback.stop();

        }

      }
      这样当这两个方法执行的时候,就可以执行我们的回调了。


Write the following statement xxx.setcallback() when glSurfaceView is initialized.
Finally, mainActivity inherits glSurfaceView.callback.
Write stop and start in the automatically generated method. What do you want to do in the corresponding time, you can realize this operation.
Now many packages The program leaves callback interfaces for you in many places. It is impossible for you to modify the corresponding program, but by modifying various interfaces, you can modify the corresponding place

To sum up
, the interface is the way that function A calls function A in function B. It sounds confusing. In fact, we cannot control two functions at the same time, so we can only set a function for the second function when it does something. Node, at this node we can perform other operations in other functions.
Do not mix in the concept of asynchrony when understanding callbacks. Many articles on the Internet that explain callbacks often like to mix in the concept of asynchrony, which makes readers unable to understand callbacks correctly.

Supongo que te gusta

Origin blog.csdn.net/qq_36740186/article/details/76436071
Recomendado
Clasificación