I'm sure you have doubts about the title, but that's exactly what the title is for Android developers. As far as I know, Android programmers are reluctant to write Findviewbyid (), click event Monitoring and other high repetition rate code. So how can we shorten the time to write business logic code and avoid writing repetitive code? So let's be an Android programmer who can be lazy and efficient. Want to know how to do it? Don't worry, I'll write about it next.
There are many third-party libraries, frameworks, and solutions available for us to use. For a variety of reasons, we do not know these libraries or know them, but they are useless. Some developers have developed their own defined libraries or do not want to use third-party libraries. If we use some third-party libraries in the process of application development, we may be able to improve the compatibility of the program, the beautiful UI interface, the neatness of the Code, and so on. In addition, in the Android application development process, we can not have too much energy and time to study the application of security this piece, to learn to borrow power! So the best way is to use a third-party platform solution , which can save a lot of time and can ensure the security of the application, lazy and efficient, the best of both worlds!
Below, let's discuss the "Dependency Injection function library".
What is dependency injection?
Dependency Injection is a software design pattern that allows you to delete and change hard-coded dependencies both at run time and at compile time. [From wikipedia] (Wikipedia resources):
Some common and popular dependency injection libraries are:
Roboguice:
Roboguice is a dependency injection framework for Android apps, and using Google's official Guice location greatly simplifies Android's dependency injection. Make your Android app development smoother, easier and more fun to program.
Do you often forget to check for NULL when you call Getintent (), Getextras () these functions? Roboguice can help you. Is initialization textview necessary to call Findviewbyid ()? No, Roboguice has done it for you.
By using Roboguice, you can inject a view view control, a resource, a system service, or any other object. Roboguice can help you streamline your application's code. Fewer code means fewer problems or bugs, so you can devote more effort to the parts of your project that need to be written or modified, making it easier to read the code.
Let's look at how the various Roboguice libraries are used.
Using the Roboguice library:
- Control Injection: initializes the control with the @injectviews method, for example: @InjectView (r.id.textview1) TextView textView1.
- Resource injection: initializes the resource with the @injectresources method, for example: @InjectResource (r.string.app_name) string name.
- system service Injection: Initializes and obtains system services using the @inject method, for example: @Inject layoutinflater inflater.
- Pojo Object injection : Use the @inject method to inject and initialize the Pojo object, for example: @Inject foo foo.
Installation
To use Roboguice, you need to download the jar files and add them to the environment variable:
- 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 take a look at a simple general event code:
Instance
<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" >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.o Ncreate (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 "); }}</span></span>
Then look at the magic of using Roboguice to streamline your code.
Using Roboguice
You have to inherit roboactivity or robofragment before you can use the Roboguice dependency injection function.
<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" >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);} } </span></span>
In such a contrast, I think you must understand why use Roboguice? Take a look at the benefits:
Benefits of using Roboguice
-
- No need to initialize system services , use @inject if necessary.
- No need to initialize like draw Able,string and other resources, use @injectresource if necessary.
- above practices can help you streamline your generation Code.
-
- A small amount of code let Andro The developer of the ID saves effort and allows them to focus more on the actual business logic.
Roboguice and Actionbarsherlock
As I mentioned earlier, you have to inherit one of the roboactivity and robofragment in order to use Roboguice in activity events or fragment. But what if you've already used Actionbarsherlock in the project to compile? The problem is that you have inherited one of the sherlockactivity or sherlockfragmentactivity. The problem now is that you can't use Roboguice and Actionbarsherlock at the same time.
The workaround is to define a base class for activities and fragments. Then you can use Roboguice and actionbarsherlock at the same time. You can take a look at the following anti-compilation solution
You can download some of the base classes here:
https://github.com/rtyley/roboguice-sherlock or download jar packs are the same:Roboguice+sherlock.jar, You can choose one to add to your project.
How to become a lazy and effective Android developer