"Android Development experience" Come, we write ourselves an Android IOC framework!

Source: Internet
Author: User

to the immediate position. Afinal Development Framework is also used for several months, remember the first time to use the gaze completion of the control initialization and event binding, when the mood is how excited-the code can actually write! And then with the continuous learning, but also slowly to the IOC framework and annotation reflection and other things have a little bit of a simple understanding. A previous article briefly introduces the reflection mechanism of java. Today's article. A simple, IOC-based demo is complete. Let us have a little bit of a simple understanding of the IOC.

First of all. What is the IOC?

Control inversion (inversion of the English abbreviation for IOC) is an important object-oriented programming principle to reduce the coupling problem of computer programs. is also the core of the lightweight spring framework. Control reversals are generally divided into two types. Dependency Injection (Dependency injection, short di) and dependent lookups.

Dependency Injection applications are more extensive.

We have to finish the following. is the implementation of dependency injection in Android.

First, look at our project structure.


The structure is very easy, a base class. A subclass, a self-defined gaze type. A layout file.

See the detailed implementation of the code below

First look at the most important base class

Baseactivity.java

Package Com.example.iocdemo;import Java.lang.reflect.field;import Android.app.activity;import Android.content.context;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.toast;public class Baseactivity extends Activity {protected Context Mcontext = this;/** * Implement IOC injection * * @par Am baseactivity */public void Initinjectedview (Object baseactivity) {//Get all member variable field[] fields = Baseactivity.getclass ( ). Getdeclaredfields (); if (Fields! = null && fields.length > 0) {//Traverse member variable for (Field field:fields) {try {//Suppress Permission Check field.setaccessible (TRUE);//Gets the member variable's gaze class viewinject viewinject = Field.getannotation (Viewinject.class);// Suppose the gaze class is not empty, that is, the member variable is declared using the Gaze method if (viewinject! = null) {//gets Idint ID in gaze = viewinject.id ();//Set field value Field.set (This, ( Activity) baseactivity. Findviewbyid (ID));//Remove the materialized View object from view view = (view) field.get (this);// Bind Listener Event View.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {toast.maketext (Mcontext, " Don't order it! "ToAst. Length_short). Show ();}});}} catch (Exception e) {e.printstacktrace ();}}}}

Inside this class. We're finished. The IOC injection method is written, with its own defined gaze type code such as the following:

Package Com.example.iocdemo;import Java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import java.lang.annotation.Target; @Target (Elementtype.field) @Retention ( Retentionpolicy.runtime) Public @interface the viewinject {public int id ();}
@Target (Elementtype.field)This code implements the control of the gaze position as a field, or as a member variable. Because we're finished with the gaze of the control. and time binding. Therefore, we set theElementtype.fieldwill be able to.

@Retention is an enum type that collectively has three values. Each is Source,class and RUNTIME.

Source represents the annotation type of information that is only kept in the program source code, which is assumed to have been compiled. The annotation data disappears and does not remain in the compiled. class file .

Class means that this annotation type of information is kept in the program source code, and at the same time it remains in the compiled. class file, and at run time, this information is not loaded into the virtual machine (JVM). Take a look. When you do not set a retention value for a annotation type. The system default value is class.
runtime, which indicates that information is kept in the source code, the compiled. class file, and that it is loaded into the JVM when it is run .

Since we need to inject the JVM after loading our class file into the entry, we select this property.

Well, now that we know how to simply define a type of gaze that we've defined, and code injection and event binding, let's look at how we use it in our program.

Here is the code for our activity

Package Com.example.iocdemo;import Android.os.bundle;import Android.widget.button;public class MainActivity extends baseactivity {//The initialization of the control with gaze @viewinject (id = r.id.btn) Button btn; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// To inject Initinjectedview (this);}}

Here are the results of our implementation


"Android Development experience" Come, we write ourselves an Android IOC framework!

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.