Annotation Frame---androidannotations

Source: Internet
Author: User

Androidannotations is an open source framework designed to accelerate the efficiency of Android development. By using its open annotations API, you can use it almost anywhere, greatly reducing the amount of innocuous code, allowing developers to get out of the room and have enough time and energy to focus on the real business logic. And by simplifying your code, it also improves the stability of the code and later maintenance costs. the following androidannotations abbreviation for AA

There may be objections, the performance of our mobile devices, no more than the background server has sufficient memory and computing power. When a lot of use of annotations, will not be the impact of the app, will not affect the performance of the app? The first clear statement here is that AA will not bring any side effects to the app, but rather its powerful and easy-to-use API will give you an unprecedented programming experience.

At present, the mainstream annotation framework has xutils, Butterknife, Dragger and Roboguice, and their implementation principles are all consistent, which is realized by reflection mechanism. By reflecting the annotated field and method in the class during runtime, the logical code corresponding to the annotation is then executed. We all know that the reflection mechanism is executed during the running of the app, which will cause the inefficiency of execution and the disadvantage of longer execution time. When you use a lot of reflection-based annotations in our app, it can seriously affect performance. But the logic of the implementation of AA is not based on this.

The AA work principle is also very simple, it uses the Java Annotation Processing Tool introduced by JDK 1.6,

An additional layer of automated compilation steps are added to the compiler to generate code based on your sources. The generated code is a direct subclass of your source, and the name of the automatically generated class is followed by an underscore after the parent class name. For example, using the @eactivity annotation myactivity, AA will automatically help you generate a class named Myactivity_ . Annotations using AA have automatically generated the corresponding subclass during compilation, and the runtime is actually the subclass, so the use of AA does not adversely affect the performance of the app.

AA Development Environment Construction: Right-click =>properties=>java Compiler = Annotation processing = Factory Path.

Let's take a quick look at this powerful framework with examples to introduce some common annotations.

I. Annotations to Components

@EActivity This annotation is used to decorate the activity, inject the layout into the activity, or you can set the page style to full screen, without title. These use with notional annotation to realize, is not very convenient AH. Support for other components is also fairly straightforward, such as @eservice, @EReceiver, @EProvider, @EApplication, @EApplication, @EFragment. You can also modify the custom control, annotated as @eview, @EViewGroup. Support is not quite comprehensive.

Ii. Annotations to resource references

With a AA, a variety of annoying findviewbyid from now on, you can simply use @viewbyid to bind the controls inside the layout, if your variable name and control ID value consistent, even the point of the ID can be omitted. And without the ID in the annotations, if the compiler cannot find the corresponding variable ID name in the r file, the compiler will give you a hint, very friendly.

At the same time you want to reference the resources in the member variables, as long as the variable is added to the corresponding annotation decoration, the same if the variable name and resource ID consistent, the ID can be omitted. Support all the resource files, all of them.

If you want to get system services, just add @systemservice annotations before your variables.

Get the value passed in the intent, plus the @extra annotation, and the fault tolerance is good, if you do not receive the key corresponding to the value, no problem, you can set the default value. Then there is a strong turn failure will not cause crash, such as the transfer is an int value, received when it is a string, there is no problem, just receive failure.

It's very powerful. The annotations that modify member variables are mainly used to solve the problem of their initialization, so that the declaration is initialized and the function can be used. There are many properties that are not introduced.

Iii. Event Binding annotations


The AA supports all basic native event bindings, as shown in the sample is three common. A simple event annotation plus a listener's control ID will complete the complex event bindings that were previously done, such as inner class implementations. And the method name can be arbitrarily customized, if the method name is consistent with the ID of the control, the ID in the annotations can also be omitted, also if the match is not on the words, the compiler is also compiled. The parameter list of the method is also customizable, and when parameters are needed, the parameter list of the native listener method is pulled directly to use. Other commonly used @textchange, @ItemClick, @SeekBarProgressChange.

Iv. interaction of asynchronous threads with the UI thread


When the view-related member variable is initialized, a method with @afterviews annotations is called, and you can initialize some interface controls inside. If the other member variables are finished, the @afterinject is called.

For example, the logic of most applications is that, after initializing the interface, it initiates time-consuming data requests, then parses the acquired data and then sets it to the interface. When it comes to interacting with the asynchronous task, the UI thread is relatively cumbersome. Let's see how AA is implemented.

Quite simply, the UI thread executes a method that adds a @uithread, the asynchronous threading method adds a @background, the interaction of the two is the method of direct mutual invocation, the other you do not care, all the implementation is a AA compiler to automatically generate interactive code. The process of interaction is completely not in the sense of performing async, no longer using handler to send a message to receive. Two annotations on the previous heap of code implementation of the function to achieve, really give the greatest praise.

Five, Rest API

The rest API is also supported in AA, and all HTTP request methods are supported. The following shows a GET request.

Define a request interface and then you can use it directly, without having to implement it yourself. This is closely related to the company's background interface design, if your company interface interaction is a restful style, you can rewrite it and enjoy the magic of AA.

written in the last : Androidannotations is a powerful, well-deserved king in the annotation framework, and the flexible API greatly improves the efficiency of development and reduces the cost of maintenance. If it has any disadvantages, I can only say no, it has no drawbacks. If hard to come to a word, it can only be its annotations more, familiar with a period of time, if the entire development team technology migration, the upfront technical cost is slightly higher. But the so-called chopping wood does not mistake sharpening, or can not be attributed to a disadvantage. AA also has a lot of powerful and practical functions, confined to the space does not expand said, to explore it yourself.

Well, that's all for today's dry goods.

AA Address: Https://github.com/excilys/androidannotations

If you find it helpful, you are welcome to subscribe to my public account-Android dry Sharing (Id:android_share). Below is the QR code, to provide you with timely high-quality Android dry.

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.