How to become a lazy and efficient Android developer and a lazy android developer

Source: Internet
Author: User

How to become a lazy and efficient Android developer and a lazy android developer

I'm sure you have doubts about this title, but this is the case. This title is perfect for Android Developers. As far as I know, Android programmers are reluctant to write code with a high repetition rate, such as findViewById () and click event listening. So how can we shorten the time for writing business logic code and avoid repeated code writing? So let's become an Android programmer who can be lazy and efficient. Do you want to know how to do this? Don't worry, I will write it later.

There are many third-party libraries, frameworks, and solutions available for us. For various reasons, we do not know these libraries or know but have never used them. Some developers develop their own libraries or do not want to use third-party libraries. If we use some third-party libraries during application development, it may improve the program's compatibility, beautiful UI, and make the code clean .; In addition, in the process of Android Application Development, it is impossible for us to spend too much energy and time studying application security! Therefore, the best solution is to use the solution of a third-party platform, which can save a lot of time and ensure the security of applications. It is both lazy and efficient!

Next, let's discuss "dependency injection function library ".

What is dependency injection?

Dependency injection is a software design pattern that allows you to delete and change hard-coding dependencies at runtime or during compilation. [From Wikipedia] (Wikipedia resources ):

Some common and common dependency injection libraries include:


RoboGuice:

Roboguice is a dependency injection framework for Android applications. It uses the official Guice library of Google to greatly simplify Android dependency injection. It makes the development of your Android app smoother and smoother, and the programming is simpler and more interesting.

When you call functions such as getIntent () and getExtras (), do you often forget to check whether the value is Null? RoboGuice can help you. Is it necessary to call findViewById () to initialize TextView? No, RoboGuice is done for you.

By using RoboGuice, You can inject View Controls, resources, system services, or any other object. RoboGuice helps you streamline application code. Less code means less problems or bugs, so you can spend more energy on the part of the project that needs to be written or modified, making it easier to read the code.

Let's take a look at the usage of various RoboGuice libraries.

Use the RoboGuice Library:

  • Control Injection:Use the @ InjectViews method to initialize the control. For example, @ InjectView (R. id. textview1) TextView textView1.
  • Resource injection:Use the @ InjectResources method to initialize resources, for example, @ InjectResource (R. string. app_name) String name.
  • System Service injection:Use the @ Inject method to initialize and obtain system services, for example, @ Inject LayoutInflater inflater.
  • POJO object injection:Use the @ Inject method to Inject and initialize the POJO object, for example, @ Inject Foo foo.

Install

To use RoboGuice, You need to download the JAR file and add them to the environment variables:

  • 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.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");     }}</span></span>

Let's take a look at the magic behind using RoboGuice to streamline code.

Use RoboGuice

You must inherit from RoboActivity or RoboFragment before using the dependency injection function of RoboGuice.

<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 this comparison, I think you must understand why RoboGuice is used? Let's take a look at the benefits:

Benefits of using RoboGuice

  • You do not need to initialize the control. Use @ InjectViews if necessary.
  • You do not need to initialize system services. Use @ Inject if necessary.
  • You do not need to initialize resources such as Drawable, string, and others. If necessary, use @ InjectResource.
  • The above practices can help you streamline your code.
  • Fewer code, fewer problems and buckets.
  • A small amount of code allows Android Developers to focus more on actual business logic while saving effort.

RoboGuice and ActionBarSherlock

As I mentioned earlier, you must inherit one of RoboActivity and RoboFragment to use RoboGuice in Activity events or Fragment. But what if you have used ActionBarSherlock for compilation in the project? The problem is that you have inherited one of SherlockActivity or SherlockFragmentActivity. Now the problem is that you cannot use both RoboGuice and ActionBarSherlock.

The solution is to define a base class for Activities and Fragments. Then you can use both RoboGuice and ActionBarSherlock. To learn about The Decompilation Solution

You can download some base classes here:
The same is true for https://github.com/rtyley/roboguice-sherlock or downloading a JAR package: RoboGuice + Sherlock. jar, you can choose one to add to your project.

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.