Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
73 views
in Technique[技术] by (71.8m points)

What are Android Annotations and what are they used for?

I want to know about Android Annotations, is it better way to use in all android projects?.

If correct, how to implement it. Is there any good tutorial for it?

If it is a wrong way. what are the drawbacks of Android Annotations?

Thanks in advance for the help.

question from:https://stackoverflow.com/questions/32482215/what-are-android-annotations-and-what-are-they-used-for

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Android Annotations is an annotation-driven framework that allows you to simplify the code in your applications and reduces the boilerplate of common patterns, such as setting click listeners, enforcing ui/background thread executions, etc.

You could go from having something like this:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView descriptionTextView = (TextView) findViewById(R.id.tv_description);
        final Button hideButton = (Button) findViewById(R.id.btn_hide);
        hideButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                descriptionTextView.setVisibility(View.INVISIBLE);
            }
        });
    }
}

To something like this:

@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

    @ViewById(R.id.tv_description)
    TextView mDescriptionTextView;

    @Click(R.id.btn_hide)
    protected void onHideButtonClick() {
        mDescriptionTextView.setVisibility(View.INVISIBLE);
    }
}

How it works

You annotate your activities and components, the annotations processor then generates classes (at compilation time) that extend your activities and components (i.e. your activities cannot be final) with an underscore suffix by default, so if you have MainActivity, now you will also have a MainActivity_ class.

This new class contains a well-written boilerplate code that does whatever the annotation specifies.

How to implement

I wrote this tutorial about how to integrate Android Annotations and even include an example on how integration tests get updated, check here.

That tutorial is valid as of today, using Android Studio ~1.5.1, and it tries to explain a little bit on how the internal works.

Should you use it?

I would say that if you have a small-medium project its fine. It will make your code easier to read. But if your application is bigger and contains a lot of navigation flows with complex activity/component life cycles, it can get a little bit hard to implement or difficult to debug and understand errors if something is not appropriately annotated.

Because of how Android Annotations operate, they embed themselves in the life cycle and doing so, you are now dependent of their lifecycle (e.g. if you annotate your views with @ViewById, then you cannot reference them in onCreate(), you need to make a method and annotate it with @AfterViews and when this method gets executed then your views are ready to be used). This is not necessarily a problem, you just need to have a good understanding of Android's behaviors and well, Android Annotations behaviors as well.

In summary, as in any library, if you depend on it, well you depend on it, so you might as well understand very thoroughly how it works. Your project now depends on someone else's.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...