Android RoboGuice2 User Guide (2): Example 1 Hello World

Source: Internet
Author: User

Download the new RoboGuice library, which consists of four libraries, as shown in:
2. Create a new Android project, such as GuiceDemo and the Target Platform Android1.5 or above.

3. Generally, you can Add a libs directory under the Project, copy two jar files to the libs directory, and then use Project> Properties> Java Build Path> Libraries> Add JARs

 

Note: From ADT17 onwards, the added jar files must be placed in the libs subdirectory. For details, see the dalvikvm: Unable to resolve superclass Problem When upgrading to ADT 17.

After adding references to the corresponding guice and roboguice libraries, you can write the first example using roboguice2.

Steps for using roboguice2:

Roboguice2 does not contain the RoboApplication class, so it is neither necessary nor possible to derive a subclass of RoboApplication. Repeat the Layout and class descriptions of HelloWorld.

1. In this simple example, Layout is defined as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: id = "@ + id/hello"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
</LinearLayout>
We have defined a TextView, whose id is hello.

Assume that this application uses an IGreetingService, And the getGreeting () method returns a string. GuideDemo does not need to care about how IGreetingService is implemented.

 

A core principle of the Dependency injection design mode is: Separate behavior from dependency resolution. This means that the functions to be implemented by the application are separated from the services or other objects on which it depends. For this example, GuiceDemo only needs to know that it depends on the IGreetingService service, but does not need to know who implements GuiceDemo for IGreetingService.

In Roboguice, @ Inject is used to represent this dependency.

[Java]
Public class GuiceDemo extends RoboActivity {

@ InjectView (R. id. hello) TextView helloLabel;
@ Inject IGreetingService greetingServce;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
HelloLabel. setText (greetingServce. getGreetings ());
}
}
Public class GuiceDemo extends RoboActivity {
 
@ InjectView (R. id. hello) TextView helloLabel;
@ Inject IGreetingService greetingServce;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
HelloLabel. setText (greetingServce. getGreetings ());
}
}
The Activity that uses RoboGuice needs to be derived from RoboActivity (RoboActivity is a subclass of Activity ).
Use @ Inject to mark greetingServce dependent on IGreetingService
Use @ InjectView to indicate that helloLabel depends on R. id. hello (XML)
The Code does not contain the code for creating a greetingServce object (such as new xxx () and the Code for assigning values to helloLabel. These values can be automatically created by Roboguice and injected into the variable by value assignment.

To illustrate the problem, we add two getGreetings implementations in the Code: HelloWorld and HelloChina:

[Java]
Public class HelloChina implements IGreetingService {

@ Override
Public String getGreetings (){
Return "Hello, China ";
}

}

Public class HelloWorld implements IGreetingService {

@ Override
Public String getGreetings (){
Return "Hello, World ";
}

}
Public class HelloChina implements IGreetingService {
 
@ Override
Public String getGreetings (){
Return "Hello, China ";
}
 
}
 
Public class HelloWorld implements IGreetingService {
 
@ Override
Public String getGreetings (){
Return "Hello, World ";
}
 
}
2. You may be confused here. How does RoboGuice know to use that class (HelloWorld or HelloChina) to assign values to greetingServce in GuiceDemo? This is implemented by defining binding in the Module.

Add a GreetingModule (derived from AbstractModule instead of AbstractAndroidModule class) to the project to overload the configure method:

[Java]
Public class GreetingModule extends AbstractAndroidModule {

@ Override
Protected void configure (){
Bind (IGreetingService. class). to (HelloWorld. class );
// Bind (IGreetingService. class). to (HelloChina. class );

}

}
Public class GreetingModule extends AbstractAndroidModule {
 
@ Override
Protected void configure (){
Bind (IGreetingService. class). to (HelloWorld. class );
// Bind (IGreetingService. class). to (HelloChina. class );
 
}
 
}
Bind IGreetingService to the HelloWorld class.

3. Define the Module in res/values/roboguice. xml

[Html] view plaincopyprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String-array name = "roboguice_modules">
<Item> com. pstreets. guice. demo. GreetingModule </item>
</String-array>
</Resources>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String-array name = "roboguice_modules">
<Item> com. pstreets. guice. demo. GreetingModule </item>
</String-array>
</Resources>
You can bind the GreetingModule to HelloChina. The comparison is as follows:

 

By changing the binding, GuiceDemo shows different results. GuiceDemo does not depend on the specific implementation, so it is very convenient to change the implementation of the interface without changing the GuiceDemo code. This greatly reduces the coupling between classes.

Author: mapdigit
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.