Android Development advanced: Google relies on injection library Roboguice

Source: Internet
Author: User

Roboguice is a dependency injection framework for Android apps, and using Google's official Guice location greatly simplifies Android's dependency injection. Make your Android app development smoother, easier and more fun to program.

What is dependency injection?

Dependency Injection is a software design pattern that allows you to delete and change hard-coded dependencies both at run time and at compile time.

Some common and popular dependency injection libraries are:

Roboguice is a dependency injection framework for Android apps, and using Google's official Guice location greatly simplifies Android's dependency injection. Make your Android app development smoother, easier and more fun to program.

Do you often forget to check for NULL when you call Getintent (), Getextras () these functions? Roboguice can help you. Is initialization textview necessary to call Findviewbyid ()? No, Roboguice has done it for you.

By using Roboguice, you can inject a view view control, a resource, a system service, or any other object. Roboguice can help you streamline your application's code. Fewer code means fewer problems or bugs, so you can devote more effort to the parts of your project that need to be written or modified, making it easier to read the code.

Let's look at how the various Roboguice libraries are used.

Using the Roboguice library:

    • Control Injection: initializes the control with the @injectviews method, for example: @InjectView (r.id.textview1) TextView textView1.
    • Resource injection: initializes the resource with the @injectresources method, for example: @InjectResource (r.string.app_name) string name.
    • system service Injection: Initializes and obtains system services using the @inject method, for example: @Inject layoutinflater inflater.
    • Pojo Object Injection : Use the @inject method to inject and initialize the Pojo object, for example: @Inject foo foo.

Installation

To use Roboguice, you need to download the jar files and add them to the environment variable:

    • Http://repo1.maven.org/maven2/org/roboguice/roboguice/2.0/roboguice-2.0.jar
    • Http://repo1.maven.org/maven2/com/google/inject/guice/3.0/guice-3.0-no_aop.jar
    • Http://repo1.maven.org/maven2/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar

Let's take a look at a simple general event code:

Instance

    public class Testactivity extends activity{TextView textView1;         TextView TextView2;         ImageView ImageView1;         String name;         Drawable Iclauncher;         Locationmanager Locmanager;         Layoutinflater Inflater;               Notificationmanager Notifymanager;             @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub             Super.oncreate (savedinstancestate);             Setcontentview (r.layout.layout_test);             TextView1 = (TextView) Findviewbyid (R.ID.TEXTVIEW1);             TextView2 = (TextView) Findviewbyid (R.ID.TEXTVIEW2);             ImageView1 = (ImageView) Findviewbyid (R.ID.IMAGEVIEW1);             Name = GetString (r.string.app_name);             Iclauncher = Getresources (). getdrawable (R.id.ic_launcher);             Locmanager = (Locationmanager) getsystemservice (Activity.location_service); Inflater = (layoutinflater) getsystemservice (Activity.             Layout_inflater_service);             Notifymanager = (Notificationmanager) getsystemservice (Activity.notification_service); Textview1.settext ("Hello world!          Roboguice demo ");  }     }


Then look at the magic of using Roboguice to streamline your code.

Using Roboguice

You have to inherit roboactivity or robofragment before you can use the Roboguice dependency injection function.

    public class Testactivity extends roboactivity{               @InjectView (r.id.textview1) TextView textView1;         @InjectView (R.ID.TEXTVIEW2) TextView textView2;         @InjectView (r.id.imageview1) ImageView imageView1;         @InjectResource (r.string.app_name) string name;         @InjectResource (r.drawable.ic_launcher) drawable iclauncher;         @Inject Locationmanager Locmanager;         @Inject Layoutinflater Inflater;         @Inject Notificationmanager Notifymanager;         @Override         protected void onCreate (Bundle savedinstancestate) {             //TODO auto-generated method stub             Super.oncreate (savedinstancestate);             Setcontentview (r.layout.layout_test);             Textview1.settext (name);         }     


In such a contrast, I think you must understand why use Roboguice? Take a look at the benefits:

Benefits of using Roboguice

    • You do not need to initialize the control and use @injectviews if necessary.
    • There is no need to initialize the system service and use @inject if necessary.
    • There is no need to initialize like drawable,string and other resources, use @injectresource if necessary.
    • These practices can help you streamline your code.
    • The less code, the less problems and bugs.
    • A small amount of code makes it easier for Android developers to focus on the actual business logic.

Roboguice and Actionbarsherlock

As I mentioned earlier, you have to inherit one of the roboactivity and robofragment in order to use Roboguice in activity events or fragment. But what if you've already used Actionbarsherlock in the project to compile? The problem is that you have inherited one of the sherlockactivity or sherlockfragmentactivity. The problem now is that you can't use Roboguice and Actionbarsherlock at the same time.

The workaround is to define a base class for activities and fragments. Then you can use Roboguice and actionbarsherlock at the same time.

You can download some of the base classes here:
Https://github.com/rtyley/roboguice-sherlock or download jar package is the same: Roboguice+sherlock.jar, you can choose one to add to your project.


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.