
For several years now, the open library for Android has been and is being improved -
Android AnnotationsIt is similar to
RoboJuice in features, but if you study it more carefully, it will become clear - it is much more abundant in features and implemented in a more convenient way for use in a project.
About this library
already wrote on Habré , but briefly, and she herself was updated.
Well, let's go through AndroidAnnotations in detail, the more she entered the
gentleman’s development kit for Android.Benefits
The first is the abbreviation of the code; you no longer need to search for the id views and separately define listeners for them.
The second is implemented, as in the most
erotic dreams of developers for Android, work with threads.
The third is a convenient and concise storage of the state of the activit / fragment
The fourth is simple handling of ListView adapters.
And a lot of different goodies ...What is inside and how it works
It works great! You do not need to inherit from RoboGreendroidOrmliteActionBarFragmentActivity and add heavyweight libraries to your project. Annotations are processed at one of the steps during compilation, only 50kb jar is added to the assembled project, the rest lies and does not fit into the assembled apk.
After the annotation Activity, during compilation AndroidAnnotations will create a successor from it, which will implement the standard Android methods. And the rest of the code that we wrote will be called from the ancestor. Because of this, we need to declare in the manifest, and MyActivity_ is a class derived from MyActivity, automatically generated by AndroidAnnotations.
It sounds scary , but in fact, thanks to this approach, we as developers will only win.
Productivity does not fall - standard methods are used. And the convenience of writing is growing many times.
And one more important feature: we don’t need to rewrite the whole project - just annotate our Activity / Fragment / View and use inside only those annotations that we need. And the rest is to write, as we used to do it earlier, and as official documentation tells us. For example, in the project only annotations for streams are needed - we add them and the Activity / Fragment annotation, and write the rest through standard methods.
Project implementation
Hereinafter, everything is described for version 2.6. Read about the changes in the next versions
.')
Project Settings
- Download the archive with the library
- androidannotations-2.6-api.jar - put it in the “libs” folder of our project.
- androidannotations-2.6.jar - put it in a new folder inside the project called “ext-libs”.
- Then go to “Project / Properties / Java compiler / Annotation Processing”.
- We tick the "Enable project specific settings", "Enable reports processing", "Enable processing in editor".
- Go to the “Factory path” sub-tab: check the “Enable project specific settings” checkbox and click “Add jar”. Add from the folder “ext-libs” “androidannotations-2.6.jar”. Click “apply” and “yes”.
- In the tab “Java Compiler” make sure that we have a “1.6” version of the compiler.
- In the tab "Java Build Path / Source" click "Add Folder" and add a new folder ".apt_generated".
- Click OK
The most tedious is over, then the song begins.
Simple activity
To begin, annotate the MainActivity - in the manifest:
<activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
replace .MainActivity with .MainActivity_
Annotate the class:
@EActivity(R.layout.main_activity) public class MainActivity extends Activity {
Now onCreate overload can be safely removed and try to compile.
Launching a subsequent Activity
In a normal Activity, the initialization method of the interface elements is always executed, and only after its execution can we call our methods that access different View. For the annotated Activity method that accesses the interface elements, we annotate it like this:
@AfterViews
Next, we will try to launch the following annotated Activity, on which we will carry out various experiments:
@AfterViews protected void afterViews(){ TestActivity_.intent(this).start(); }
This is the easiest way to start annotated Activity.
To run with
TestActivity_.intent(context).myDateExtra(someDate).start();
extras:
TestActivity_.intent(context).myDateExtra(someDate).start();
where myDateExtra is an annotated
@ Extra
serializable protected-field in TestActivity
To run with flags:
TestActivity_.intent(context).flags(FLAG_ACTIVITY_CLEAR_TOP).start();
Annotate Activity to the full
Search View:
@ViewById protected EditText testactivity_first_edittext; @ViewById(R.id.testactivity_second_textview) protected TextView secondTextView;
To save the state, you do not need to manually remove the saved objects of the instance state;
@InstanceState String stateToSave;
And when the state is changed, for example, when the screen is rotated, this field will not be recreated, but will retain its value.
Getting resources, for example strings:
@StringRes(R.string.hello_world) String myHelloString;
We hang onClickListener:
@Click(R.id.testactivity_first_button) void myButtonWasClicked() { secondTextView.setText("first button was clicked"); }
Listen to text changes:
@TextChange(R.id.testactivity_second_edittext) void onTextChangesOnSomeTextViews(TextView tv, CharSequence text) { Toast.makeText(this, "second textview was changed", Toast.LENGTH_SHORT).show(); }
Best work with threads
Basically, I use this library in my projects for working with streams.
Two annotations are used for this:
@ Background
- the annotated method will be executed in the background
@ UiThread
- this is how the method called in the background thread but running in the
UI thread should be annotated
Here is the example code:
@Click(R.id.testactivity_second_button) protected void secondButtonWasClicked() { backgroundWork(); } @Background protected void backgroundWork() { publishProgress(0); publishProgress(10); publishProgress(100); onBGTaskFinish("bg task finished"); } @UiThread void publishProgress(int progress) { testactivity_first_edittext.setText("Background task progress: "+ progress); } @UiThread void onBGTaskFinish(String resultText){ secondTextView.setText(resultText); }
At the click of a button, a background thread is launched, which, during its execution, publishes progress and eventually transmits the result of execution.
That's all for today, to be continued ...
LibraryDescription of all library annotationsQuickly integrate the library into a new project.Project example from the article