Introduction to RoboGuice with a brief example

09 / Feb / 2015 by Anand Rai 0 comments

Have you heard about RoboGuice? Well, don’t be surprised. It is now getting familiar like other things in Android development.

RoboGuice uses Google Guice library, a simple framework and easy-to-use dependency injection to Android.

Just think about casting a view using findViewById() and it is so necessary, for which you have a one new thing i.e. RoboGuice. RoboGuice gives you a full pleasure to stay out of this in your Android development experience and makes things simple and fun. May be possible, you always forget to check null for getIntent().getExtra(). RoboGuice will help you.

No more guesses in development, that you have to inject your View, Resource, System Service, or any other object, and let RoboGuice take care for all whatever you want.
“There’ is no “magic””. The thing you want is configured explicitly by RoboGuice or can be overridden by you.

Just have a look on some code, you will get the idea. Here is the example of an activity.

class RoboExampleActivity extends Activity { 

	TextView txtName;
        ImageView imgProfile;
        LocationManager loc;
        Drawable icon;
        String userName;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            txtName      = (TextView) findViewById(R.id.txt_name);
            imgProfile = (ImageView) findViewById(R.id.img_profile);
            loc       = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
            icon      = getResources().getDrawable(R.drawable.ic_launcher);
            userName    = getString(R.string.app_name);
            txtName.setText( "Hello, " + userName);
        }
    } 

This example has 19 lines of code. If you are trying to read from onCreate(), you have to skip over 5 lines of code initialization to find the only thing that matters: txtName.setText(). And complex activities can end up with a lot more of this sort of initialization code.

Compare this to the same app, written using RoboGuice:

 @ContentView(R.layout.main)
    class RoboExampleActivity extends RoboActivity {
        @InjectView(R.id.txt_name)             	TextView txtName;
        @InjectView(R.id.img_profile)        	ImageView imgProfile;
        @InjectResource(R.drawable.ic_launcher) Drawable icon;
        @InjectResource(R.string.app_name) 	String userName;
        @Inject                            	LocationManager loc; 

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            txtName.setText( "Hello, " + userName);
        }
    }

Did you notice any thing? Did you try to compare?

…..
……..

Let me help you. In this example, onCreate() is much easier to understand at a glance. All the platform extra work is discarded and you have just your own app’s main business logic. If you need a SystemService? Inject it. if you need a View or any Resource? you can Inject those, too, and RoboGuice will do all.

RoboGuice’s main objective is to make your code only you app, rather than be about all the initialization and life cycle code you typically have to maintain in Android.

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

As I mentioned earlier that RoboGuice provides all classes for using fragments and actionbar
It also available with sherlock action bar.

Get all related classes from GitHub
Have a nice time with RoboGuice. ;)

Anand Rai
My Linkedin
My Stackoverflow
My Twitter

Tag -

FOUND THIS USEFUL? SHARE IT

Leave a comment -