What is RoboActivity? Guicing up Activity

This is my second RoboGuice tutorial on configuring Android applications with RoboGuice (Dependency Injection (DI) for Android). If you are not aware of what DI or RoboGuice is, you can check out here!
In this tutorial, I will introduce you to configuring Android Activities with RoboGuice and its advantages over conventional coding.
RoboGuice - RoboActivity
RoboGuice - RoboActivity
Configuring Activity with RoboGuice:
For the RoboGuice DI framework to inject the dependencies into your Activity, your Activity should extend the RoboActivity class. Thats it..!!

RoboActivity vs Activity:
Lets have a glance at the advantages we derive from using RoboActivity over the conventional Activity:
  1. You need not worry about the initializations of your views and resources. It injects them for you automatically.
  2. It reduces the number of lines of code you write. The more the code - the more the chances of bugs. RoboGuice takes care of the mechanics and you can concentrate on the actual business logic.
So, how does it do all these things?

Lets take an example of a simple conventional Android Activity with a TextView and a Button.

Conventional way:
public class ActivityExample extends Activity {

    private TextView mTextView;
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_robo_activity_example);

        mTextView = (TextView) findViewById(R.id.text_view);
        mButton = (Button) findViewById(R.id.button);

        mTextView.setText("Hello RoboGuice World...");
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getBaseContext(), "Button clicked", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

In this RoboGuice tutorial, just have a look at the code. The code of getting the resources via the method findViewById() and typecasting them to the respective types can actually be auto-generated! Because, it is obvious that we want to have the resource R.id.text_view as a TextView object. So, if someone smart enough sits over there, can easily guess this and do it for us. RoboGuice exactly does the same thing for us!

RoboGuice way:
@ContentView(R.layout.activity_robo_activity_example) // same as setContentView(R.layout.activity_robo_activity_example);
public class RoboActivityExample extends RoboActivity {

    @InjectView(R.id.text_view) // (TextView) findViewById(R.id.text_view)
    private TextView mTextView;
    @InjectView(R.id.button)
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTextView.setText("Hello RoboGuice World...");
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getBaseContext(), "Button clicked", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Now you get the thing. Here, we can straightaway concentrate on setting the text view and implementing a listener instead of writing all the boilerplate code of initializing the resource objects.

This all looks simple and conventional coding is no big deal compared to this. But, think about your class growing like anything into a complex body. It becomes very difficult to maintain it in conventional coding, which is not with RoboGuice. RoboGuice even lets you modularize your code, allowing your classes to be loosely coupled and thus easy to maintain. I will introduce you to such concepts in my next posts.

Stay tuned for more Robo stuff.

You can get an example of the above mentioned RoboGuice tutorial here.

Please allot two minutes to provide feedback below. Thank you!

HAPPY CODING...!! :)

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. good one prakash..... going to implement , I thought , Using this line of code may be decrease.is there any effect on performance of the app???

    ReplyDelete