On the callback thought in the design of Android system

Source: Internet
Author: User
Tags call back thread

Why do I write this article

As I get closer to Android development, I'm increasingly discovering that Android (at least the application that used to have it) uses a lot of callback thinking. For example, the life cycle of activity, fragment life cycle, is the callback function implementation, Android, the event processing mechanism is a callback, the thread of asynchronous tasks, loader, Hanlder, etc. are based on callback, and so on.

One of the fundamental ideas of Android is the callback, which requires you to participate, call back the relevant callback method, and you populate it.

The idea of a callback can be said to infiltrate all aspects of the Android design, and almost all of the Android development methods are callback methods. As a novice who is committed to further learning about mobile development, it is necessary to understand the callback in depth.

Ii. why the callback should be introduced

A callback is a design strategy that is not possible, imagine a system implementation: In a download system has a file download module and a download file current progress Display module, the system requires real-time display file download progress, think very simple, in the object-oriented world is nothing more than the implementation of two classes just. But the problem is just here, how does the display module drive the download progress bar? The display module does not know and should not know download the module is aware of the file download progress (object-oriented design package, module to decouple, within the module to gather), the file download progress is only the download module to know things, The solution is simply to pass a function pointer to the download module as a callback function to drive the display of the display module.

In the object-oriented world, such a lot of examples, causing the root causes of such problems, I believe that we have learned from the above narrative, is the object-oriented program design idea, is the design pattern required module independence, cohesion well-structured classes low coupling characteristics.

Package Change programming strategy The first guiding principle for programmers is interface-oriented programming, which is referred to in design patterns for virtual programming rather than implementation oriented. This kind of programming thought has greatly reformed the programming world, can say without this principle there is no object-oriented programming, this principle gives a guiding ideology to the program design: namely how to map the reality model to the program model more highly. such design ideas, while greatly creating highly independent modules, weaken the collaboration between modules, that is, coupling, it makes the module more engaged in one-way call work, a module needs some kind of service to find another module, which makes the program presents a hierarchy, high-level through the interface call the bottom, Provide services at the bottom. However, the real world strictly follow the current hierarchical characteristics of the system is very rare, absolute MVC does not exist, because more modules require communication and collaboration, there is no coupling there is no collaboration without good call relationship, coupling is really not wrong.

Now that we need the collaboration between modules, and we loathe to abandon the module between you have me and I have your ambiguous relationship that how to generate the system, the answer is the function pointer (not necessarily a function pointer) is the way to use callbacks. if an object cares about the state change of another object, then register the callback function with the change of state so that it informs you of the change of the state, so as to encapsulate the change of the module and realize the collaborative relationship between the modules and decouple the objects separately.

Iii. the popular understanding of callback

You're hungry, you want to eat, just a little while to ask your mother, "dinner is not ah?" This is the normal function call.

But today your mother is making dumplings, it takes a long time, you run and run, it bothers you. So you say to your mother, I go out to play, dinner time to call my cell phone. After a while, your mother called you to say, "dinner, come back to eat!"

In which, you tell your mom to call you on the phone, is that you save the callback function handle to your mother's action. Your mother called you, is a callback process.

Iv. detailed explanation of the callback

Understanding Mode 1: General Write program is you call the system API, this process called call. If you turn the relationship back, you write a function that lets the system call your function, which is the callback (CallBack), the function called by the system is the callback function. In Android, we typically call the Framwork-defined API, but if we write a method that is called by Framwork, this method is the callback function.

Understanding Mode 2: The so-called callback function, the way it works is, "You don't have to call me, wait for me to call you." A callback means that you succeed after you execute a statement, and then perform the function you pass in, which is especially useful when doing asynchronous processing.

Understanding Mode 3: To the nature of the program, excluding those object-oriented concepts, the callback function is a function through the function pointer call. is to use function pointers to do parameters. The function pointer is the callback function.

Callbacks are programming-level design patterns, not language based. In C + +, to use the callback function, the called function needs to tell the caller his or her own pointer address, but there is no pointer in Java, what to do? We can implement the definition of callback functions through an interface (interface). Delegate ==callback in the. NET world.

How do you use callbacks in Android?

Java callback is implemented through interfaces.

Cases:

1.class A,class B

2.class a implements interface operate

3.class B has a function with a parameter of operate interface type Test (operate O)

4.class a runtime calls the test function in Class B to pass in the argument itself

5.class B has obtained a, you can recall the method in the operate interface implemented by a at any time

Vi. Use of cases

I have written a blog for the case to do analysis:

http://blog.csdn.net/a910626/article/details/45767079

Callback diagram:

Vii. Where to call back

The callback function is used primarily for some of the more time-consuming operations, or in response to events that do not know when it will occur, and where asynchronous binding (in fact, a single thread is re-opened).

Callback functions are often used when two objects need to be coordinated, such as when an object wants to get some data inside another object.

Viii. Advantages of the callback

Callback functions make programming more flexible.

The realization of module independence, the realization and the caller separation, in line with the object of high cohesion low coupling design idea.

Ix. shortcomings of the callback

The readability of the code is bad.

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.