About Android Callback

Source: Internet
Author: User

About Android Callback

1. Introduction, Example

 

CallBack is the meaning of CallBack. CallBack functions or CallBack methods are a very important concept in software design and development. For programmers, understanding the idea of CallBack functions (no matter which language is used) is necessary.

What is a callback function? The callback function is a function reserved for system calls, and we know the time when the function is called. There are two points to note: First, we write a callback function not to call it for ourselves, but to call it for the system at a certain time in the future. Second, we should know under what circumstances the system will call the callback function we have written.

Let's take a callback function in real life as an example. What is the first thing we do in normal times? That's right. It's the student ID and name. Note that the student ID and name we enter are not displayed for ourselves (that is, this method is not called for ourselves ), this is actually a callback application.

Next, let's take a look at the app callback scenario in Android.

Scenario 1:

 

Button button = (Button) this. findViewById (R. id. button); button. setOnClickListener (new Button. onClickListener () {// callback function @ override publicvoid onClick (View v) {buttonTextView. setText (the button is clicked );}});

 

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

Scenario 2:

 

@Overridepublicvoid onCreate(Bundle saveInstanceState) {  super.onCreate(saveInstanceState);  // You code...}@Overridepublicvoid onResume() {  super.onResume();  // You code...}

 

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 lifecycle, the Android system will automatically call the corresponding method (onCreate, onPause, onResume, onDestroy, etc)

The above are the scenarios where callback is used in two Android systems. Their code implementation may be different, but their thoughts are similar, which are the embodiment of callback ideas. Below, we simulate these two scenarios in Java respectively.

First, register the event listener. First write a listener Interface

 

Package com. listener;/*** click listener interface **/publicinterface MyOnClickListener {publicvoid onClick ();}

 

Then write our own Button class.

 

Package com. listener; publicclass MyButton {private MyOnClickListener listener;/*** set click listener * @ param listener click listener implementation class */publicvoid setOnClickListener (MyOnClickListener listener) {this. listener = listener;}/*** button is clicked */publicvoid doClick () {listener. onClick ();}}

 

Finally, register the listener on the Client side and trigger the click operation.

 

Package com. listener; publicclass Client {publicstaticvoid main (String [] args) {MyButton button = new MyButton (); // register the listener button. setOnClickListener (new MyOnClickListener () {@ Override publicvoid onClick () {System. out. println (the button is clicked) ;}}); // simulates the user to click the button. doClick ();}}

 

The above is the application of callback ideas in Android event listening. We then simulate the second scenario, and the embodiment of callback in the activity life cycle method call. Because it is relatively simple, I will not explain it more. You can directly read the code.

 

Package com. activity; public abstract class Activity {protectedvoid onCreate () {System. out. println (creation preparation ~~~~~~~); } Protectedvoid onDestroy () {System. out. println (destruction preparation ~~~~~~~~); }}
Package com. activity; publicclass ConcreteActivity extends Activity {@ Override protectedvoid onCreate () {super. onCreate (); System. out. println (creating !!!); } @ Override protectedvoid onDestroy () {super. onDestroy (); System. out. println (destroying !!!); }}
package com.activity;publicclass Client {    publicstaticvoid main(String[] args) {        Activity activity =new ConcreteActivity();        activity.onCreate();        activity.onDestroy();    }}

 

 

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.