[share]How to Become a Lazy but Productive Android Developer

How to Become a Lazy but Productive Android Developer

How can we optimize throughput time for writing the business logic for the app and avoid lazy code to write?

by

Paresh Mayani

·

Dec. 16, 13 · Mobile Zone

Like (1)

Comment (3)

Save

Tweet

21.63k Views

I am sure you might be wondering and thinking about the title of the post :), but yes, the title is perfectly suited to developers. As far as I know, we Android developers are 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 am going to write about.

There are many 3rd party libraries and frameworks available, either we don’t know about it or we haven’t used or we have defined 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 app development then we could achieve tasks like providing compatibility, cool and polished UI, clean code, etc. So I would be exploring more about such libraries which may 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 Injection 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 you always forget to check for null when you getIntent().getExtras()? RoboGuice 2 will 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 you to 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 you to generate readable code which could be 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 textView1;
  • 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 an example of general activity code:

Example

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");
    }
}

Let’s check the magic of RoboGuice and let’s slim down the code.

Using RoboGuice

To use RoboGuice for the dependency injection, you are required to extends either RoboActivity or RoboFragment.

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);
    }
}

Now I am sure you 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 you 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 you are required to extend either RoboActivity or RoboFragment to use RoboGuice in either Activity or Fragment particularly. But what if you have included ActionBarSherlock in your project to manage compatibility? The problem is you have extended either SherlockActivity or SherlockFragmentActivity for your activity or fragment particularly and now the situation is we could not use RoboGuice with ActionBarSherlock.

But There is one solution: You have to define custom base classes for Activities and Fragments to use RoboGuice and ActionBarSherlock both.

You can download base classes here: https://github.com/rtyley/roboguice-sherlock or download a JAR file for the same: RoboGuice+Sherlock.jar, include either of them in your project.

I think I am done exploring everything about RoboGuice and its usage/benefits in Android apps. If any point is left out, then please comment. In the next article, I will be exploring another library that can make you lazy but a productive android developer

时间: 2025-01-04 13:18:08

[share]How to Become a Lazy but Productive Android Developer的相关文章

[it-ebooks]电子书列表

#### it-ebooks电子书质量不错,但搜索功能不是很好 #### 格式说明  [ ]中为年份      ||  前后是标题和副标题  #### [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Objective-C to develop iPhone games http://it-ebooks.info/book/3544/ Learning Web App Developmen

Electronic Payment App analysis

Electronic Payment App is getting more and more popular now. People don't have to bring credit cards any more. All they need to do is using their smartphones and they could go shopping, check bills and dining in restaurants. It very convenient but so

Fresco介绍:Android的一个新图片库

翻译自:https://code.facebook.com/posts/366199913563917 快速有效的展示图片对Facebook Android客户端非常重要.可是该团队多年来在有效存储图片时遇到了很多问题.图片很大,可是设备却很小.每个像素需要占用4字节的数据----red,green,blue和alpha值各占一字节.如果手机屏幕的尺寸是480*800的话,一张全屏的图片会占用1.5M的内存.而手机的内存是有限的,Android设备给它的众多应用程序分配各自的内存空间.在有些设备

(转) [it-ebooks]电子书列表

[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Objective-C to develop iPhone games http://it-ebooks.info/book/3544/Learning Web App Development || Build Quickly with Proven JavaScript Techniques http://

linux 下 TeXmacs 作 Mathematica 10 的前端

TeXmacs可以作很多种数学软件的前端,比如maxima,octave,R等.甚至还可以作mathematica的前端.TeXmacs的mathematica 插件比较老,默认条件下无法运行mathematica 10.以下假设TeXmacs与mathematica已正确安装在64位linux下的/usr/local目录中,需对以下文件作修改(其中橙色部分为修改内容): 1. /usr/local/libexec/TeXmacs/bin/tm_mathematica #!/bin/sh err

Android基础之Intent 和 Intent 过滤器

Intent是一个消息传递对象,您可以使用它从其他应用组件请求操作.尽管 Intent 可以通过多种方式促进组件之间的通信,但其基本用例主要包括以下三个: 1.启动 Activity: Activity 表示应用中的一个屏幕.通过将 Intent 传递给 startActivity(),您可以启动新的 Activity 实例.Intent 描述了要启动的 Activity,并携带了任何必要的数据.如果您希望在 Activity 完成后收到结果,请调用 startActivityForResult

《2015最新Android基础入门教程》完结散花~

<2015最新Android基础入门教程>完结散花~ 标签(空格分隔): 反思小结 引言: 从六月底就开始编写这套教程,历时将近五个多月,今天终于写完了,全套教程正文部分148篇, 十大章,从基本UI控件到四大组件,Intent,Fragment,事件处理,数据存储,网络编程,绘图与动画, 多媒体,系统服务等都进行了详细的讲解!代码都是都是在Android Studio上进行编写的,全文 采用Markdown,行文结构清晰,还结合了实际开发中一些常见的问题进行了剖析-由于个人能力的局限, 虽然

Ubuntu下Android Studio安装、配置和使用

Ubuntu下使用Android Studio开发应用程序,首先需要安装该IDE. 情况1:Ubuntu下安装Android Studio 打开Terminal,执行下述指令: sudo apt-add-repository ppa:paolorotolo/android-studio sudo apt-get update sudo apt-get install android-studio 在执行步骤1时,James的情况是: 让用户选择执行:Enter(继续执行)或者Ctrl+C(终止执

一行代码引入 ViewPager 无限循环 + 页码显示

(出处:http://www.cnblogs.com/linguanh) 前序: 网上的这类 ViewPager 很多,但是很多都不够好,体现在 bug多.对少页面不支持,例如1~2张图片.功能整合不全(无限+页码)等等,本类由我从零到无完成的,基本已找完 bug,注释丰富,方便大家理解. 特点: 1,代码量少 , 共两个类,约合 310 行代码 (除去注释) 2,可扩展 , 再加个 handler 即可实现自动轮播 3,时间复杂度低 4,耦合度低,只依赖了 imageLoader,可以自己切换