标签:
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.
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 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.
To use RoboGuice, you need to download following JAR files and add them to your classpath:
Let’s look at an example of general activity code:
public class TestActivity extends Activity{
TextView textView1;
TextView textView2;
ImageView imageView1;
String name;
Drawable icLauncher;
LocationManager locManager;
LayoutInflater inflater;
NotificationManager notifyManager;
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.
To use RoboGuice for the dependency injection, you are required to extends either RoboActivity or RoboFragment.
public class TestActivity extends RoboActivity{
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:
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
[share]How to Become a Lazy but Productive Android Developer
标签:
原文地址:http://www.cnblogs.com/slyrx/p/5059149.html
Comment (3)