[Share] How to become a Lazy but productive Android Developer

Source: Internet
Author: User

How to become a Lazy but productive Android developerhow can we optimize throughput time for writing the business logic fo R the app and avoid lazy code to write? ByParesh Mayani·Dec. 16, 13 · Mobile Zonelike (1)

Comment (3)

SaveTweet21.63k views

I am sure you might was wondering and thinking about the title of the post:), but yes, the title was perfectly suited to de Velopers. As far as I know, we Android developers is lazy in writing code like Findviewbyid (), click Listeners, etc. So, how can we optimize throughput time for writing the business logic for the app and avoid such code to write? Let's be lazy but productive Android developers. How? Well, that's what I'm going to write about.

There is many 3rd party libraries and frameworks available, either we don ' t know about it or we haven ' t used or we have D Efined our own libraries or we haven ' t thought about to use such libraries due to ' n ' number of reasons. If we use some of them in Android apps development then we could achieve tasks like providing compatibility, cool and Polis Hed UI, clean code, etc. So I would is exploring more about such libraries which could help novice and experienced developers.

Let's talk about Dependency injection libraries today.

What is Dependency injection?

Dependency injection is a software design pattern that allows the removal of hard-coded dependencies and makes it possible To change them, whether at run-time or compile-time. [Source:wikipedia]

Some of the popular and widely used Dependency injection Libraries are:

Roboguice:

Roboguice

Roboguice is a dependency injection framework for Android applications that brings the simplicity and ease of dependency I Njection to Android, using Google ' s own Guice library. It smoothes out some of the wrinkles in your Android app development experience make things simple and fun. Do all forget to the check for null if you Getintent (). Getextras ()? Roboguice 2 would help you. Think Casting Findviewbyid () to a TextView shouldn ' t be necessary? Roboguice is on it.

Using Roboguice, you can inject your View, Resource, System Service, or any other object. Roboguice can help with slim down application code. Less code means fewer chances of issues/bugs and can help you to save efforts required to write or modify particular code In a project. IT helps generate readable code which could is easier to follow.

Let's look at different usage of the Roboguice library.

Usage of Roboguice Library:
    • Views injection:: To initialize views, use @InjectViews, for example: @InjectView (r.id.textview1) TextView TEXTV Iew1;
    • Resources Injection: To initialize and get resources, use @InjectResources, for example: @InjectResource (r.string.app_name) string name;
    • System Services Injection: To initialize and access system services, use @Inject, for example: @Inject layoutinflater inflater;
    • POJO Object injection: To inject and initialize POJO object, use @Inject, for example: @Inject foo foo;
Installation

To use Roboguice, you need to download following JAR files and add them to your classpath:

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 look at a example of general activity code:

Example
activity{
    TextView1;
    TextView2;
    ImageView1;
    Name
    Iclauncher;
    Locmanager;
    Inflater;
    Notifymanager;
    @Override
    OnCreate (savedinstancestate) {
        TODO auto-generated Method Stub
        Super. onCreate (savedinstancestate); 
        Setcontentview (R.layout.  layout_test);  
        = (findviewbyid (R.id).  TEXTVIEW1);   
        = (findviewbyid (R.id).  TEXTVIEW2);   
        = (findviewbyid (R.id).  IMAGEVIEW1);   
        GetString (R.string.  app_name);  
        Getresources (). getdrawable (R.ID.  Ic_launcher);   
        = (getsystemservice (Activity) . Location_service);  
        = (getsystemservice (Activity) . Layout_inflater_service);  
        = (getsystemservice (Activity) . Notification_service);  
        TextView1. SetText ("Hello world! Roboguice demo "); 
    }
}

Let's check the magic of Roboguice and let's slim down the code.

Using Roboguice
to use Roboguice for the dependency injection, you is required to extends either roboactivity or C14>robofragment.
roboactivity{
    @InjectView (R.ID.  TextView1;  
    @InjectView (R.ID.  textView2;  
    @InjectView (R.ID.  ImageView1;  
    @InjectResource (R.string.  name;  
    @InjectResource (R.drawable.  Iclauncher;  
    Locmanager;
    Inflater;
    Notifymanager;
    @Override
    OnCreate (savedinstancestate) {
        TODO auto-generated Method Stub
        Super. onCreate (savedinstancestate); 
        Setcontentview (R.layout.  layout_test);  
        TextView1. SetText (name); 
    }
}

Now I am sure your could realize benefits of using roboguice, so let's talk about benefits:

Benefits of Using Roboguice
    • No need to initialize views, if you want then Inject it using @InjectViews
    • No need to initialize System service. If you need then Inject it using @Inject
    • No need to initialize drawable, string and other resources. If you require then Inject it using @InjectResource
    • All of these above practices helps your to slim down the code.
    • Less code = Less chances of bugs/issues
    • Less code = Android Developer can save coding efforts and can focus fully on actual business logic of Android app
Roboguice and Actionbarsherlock

As I mentioned earlier, in alert box is required to extend either roboactivity or robofragment to use Roboguice in EI Ther Activity or Fragment particularly. But what if are you having included Actionbarsherlock in your project to manage compatibility? The problem is a extended either sherlockactivity or sherlockfragmentactivity for your activity or fragment Particu Larly and now the situation are we could not use Roboguice with Actionbarsherlock.

But there is one solution:you has to define custom base classes for activities and fragments to use Roboguice and Action Barsherlock both.

Can download base classes Here:https://github.com/rtyley/roboguice-sherlock or download a JAR file for the Same:robo Guice+sherlock.jar, include either of them in your project.

I think I am done exploring everything on roboguice and its usage/benefits in Android apps. If any, the comment. In the next article, I'll be exploring another library that can make you lazy but a productive Android developer

[Share] How to become a Lazy but productive 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.