How to become a lazy and effective Android developer

Source: Internet
Author: User

I'm sure you have doubts about the title, but that's exactly what the title is for Android developers. As far as I know, Android programmers are reluctant to write Findviewbyid (), click event Monitoring and other high repetition rate code. So how can we shorten the time to write business logic code and avoid writing repetitive code? So let's be an Android programmer who can be lazy and efficient. Want to know how to do it? Don't worry, I'll write about it next.

There are many third-party libraries, frameworks, and solutions available for us to use. For a variety of reasons, we do not know these libraries or know them, but they are useless. Some developers have developed their own defined libraries or do not want to use third-party libraries. If we use some third-party libraries in the process of application development, we may be able to improve the compatibility of the program, the beautiful UI interface, the neatness of the Code, and so on. In addition, in the Android application development process, we can not have too much energy and time to study the application of security this piece, to learn to borrow power! So the best way is to use a third-party platform solution, which can save a lot of time and can ensure the security of the application, lazy and efficient, the best of both worlds!

Below, let's discuss the "Dependency Injection function library".

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. [From Wikipedia] (Wikipedia resources):

Some common and popular dependency injection libraries are:

Roboguice:

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

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" > Public classTestactivityextendsactivity{TextView TextView1;    TextView TextView2;    ImageView ImageView1;    String name;    Drawable Iclauncher;    Locationmanager Locmanager;    Layoutinflater Inflater;     Notificationmanager Notifymanager; @Overrideprotected voidonCreate (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 "); }}</span></span>

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.

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" > Public classTestactivityextendsroboactivity{@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; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.layout_test);    Textview1.settext (name); }}</span></span>

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 take a look at the following anti-compilation solution

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.

How to become a lazy and effective Android developer

Related Article

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.