Six things you need to know before developing your first app

Six things you need to know before developing your first app

[[143903]]

My first app was terrible. In fact, it was so terrible that I removed it from the App Store and I won't even list it on my resume. It wouldn't have been this terrible if I had known a few things about Android development before I started.

This is a list of things to keep in mind when developing your first Android app. The actual mistakes I will show you below were made in my first app code. Keeping these mistakes in mind can help you develop an app you can be proud of.

Of course, as codestandards says: "If the work you do is anything like the Android apps you developed as a student, there's a good chance you'll hate your apps."

If the code you wrote a year ago feels good to you, you probably haven’t been learning enough.

-Code Standards 2015.5.21

If you are an experienced Java developer, there is a good chance that 1, 2, and 5 will not appeal to you. On the other hand, even if you have never made the mistakes in these examples, 3 and 4 may show you some cool things you can do with a piece of software you may not know about, Android Studio.

1. Don’t hold a static reference to the Context

  1. public   class MainActivity extends LocationManagingActivity implements ActionBar.OnNavigationListener,
  2. GooglePlayServicesClient.ConnectionCallbacks,
  3. GooglePlayServicesClient.OnConnectionFailedListener {
  4. //...  
  5. private   static MeTrackerStore mMeTrackerStore;
  6. //...  
  7. @Override  
  8. protected   void onCreate(Bundle savedInstanceState) {
  9. //...  
  10. mMeTrackerStore = new MeTrackerStore( this );
  11. }
  12. }

This may seem like an impossible mistake to make for everyone. It's not, I've made it. I've seen others make it, and I've interviewed people who couldn't quickly figure out why it was a mistake to put it in the first place. Don't do it, it will change.

If MeTrackerStore keeps a reference to an Activity through its constructor, the Activity will not be garbage collected (GC). (Unless the static variable is reassigned to a different Activity). This is because mMeTrackerStore is a static variable, and the memory of static variables is not reclaimed until the application exits. If you are trying to do something like this, there is a good chance that your code is seriously wrong. Seek help, perhaps Google's Udacity course "Android Development for Beginners" can help you.

Note: Technically, you can have a static variable reference to an Application Context without causing a memory leak, but I don't recommend it.

2. Beware of implicit references to objects whose lifecycle you don’t control

  1. public   class DefineGeofenceFragment extends Fragment {
  2. public   class GetLatAndLongAndUpdateMapCameraAsyncTask extends AsyncTask<String, Void, LatLng> {
  3.   
  4. @Override  
  5. protected LatLng doInBackground(String... params) {
  6. //...  
  7. try {
  8. //Here we make the http request for the place search suggestions  
  9. httpResponse = httpClient.execute(httpPost);
  10. HttpEntity entity = httpResponse.getEntity();
  11. inputStream = entity.getContent();
  12. //..  
  13. }
  14. }
  15. }
  16. }

There are many problems with this code, I will focus only on the "implicit reference" ones. In Java, a (non-static) inner class has an implicit reference to the outer class instance.

In this example, any GetLatAndLongAndUpdateCameraAsyncTask has a reference to the outer class DefineGeofenceFragment. The same is true for anonymous classes, they also have an implicit reference to the instance of the class that contains them.

GetLatAndLongAndUpdateCameraAsyncTask has an implicit reference to a Fragment object whose lifecycle we cannot control. The Android SDK is responsible for creating and destroying Fragments. If GetLatAndLongAndUpdateCameraAsyncTask cannot be garbage collected because it is running, then DefineGeofenceFragment will also remain and cannot be garbage collected because of the implicit reference.

Here's a great Google video explaining why this happens.

3. Work with Android Studio

This is the code I generated in Android Studio using "Generate Getter". These getters hold the instance variables with the 'm' prefix, and it's no stretch to think that the same can be done for a method as well.

(If you're wondering why 'm' is the first letter of the instance variable's name, 'm' is the accepted convention for instance variables. It stands for 'member').

Regardless of whether you think prefixing instance variables with 'm' is a good idea, know that Android Studio can help you follow any accepted conventions you want to implement. For example, you can use a setting in the Android Studio Code Style dialog box to have Android Studio automatically add 'm' to your instance variables and remove 'm' when you generate getters, setters, and constructor parameters for them.

Android Studio can do much more than that. Learning Android Studio is a good start by learning shortcuts and templates.

4. A function should do only one thing

In one of the many classes I write, there is a method I wrote that is over 100 lines long. These types of methods are very difficult to read, modify, and reuse. Try to have methods do only one thing. Obviously, this means you should be skeptical of methods that are over 20 lines long. Here, you can use Android Studio to help you find problematic methods:

5. Learn from smart and experienced people

This may sound trivial, but this was a mistake I made when developing my first app.

When you develop an application, you will make mistakes that others have made. By learning from others, you can save yourself time by avoiding the mistakes that others have made. I wasted a lot of time on my first application making mistakes that I could have avoided if I had taken the time to learn from experienced software developers.

Read Pragmatic Programmer, then read Effective Java. These two books will help you avoid common mistakes made by new developers. After you have studied these two books, keep looking for smart people and learn from them.

6. Use Class Libraries

When you develop an application, you will probably encounter problems that have already been solved by smart and experienced people. Moreover, many solutions to these problems are available as open source libraries, so take advantage of them.

In my first app, I wrote code that used functionality provided by some libraries. Some of these were standard Java libraries, and some were third-party libraries like Retrofit and Picasso. If you are not sure what library you are using, there are three things you can do:

  1. Listen to the Google IO Fragmented broadcasts. During these sessions, ask these developers what third-party libraries are important for Android.
  2. Subscribe to Android Weekly. This contains some of the most popular libraries, so always keep an eye out for what you find useful.
  3. Look for open source applications that solve problems similar to the ones you encounter in your application development. You may find that an application uses a third-party library that is exactly what you want, or you may find a Java library that you didn't know about.

Summarize

Developing good Android apps is hard. Don't make the same mistakes that I made before. If you find a mistake in my code, please let me know in the comments. (Misleading comments are worse than no comments at all). If you think this article is useful for new developers, please share it. It can help them solve some of their headaches.

<<:  Will I definitely learn some cool skills if I go to a big bat company?

>>:  Mathematics and Programming

Recommend

2022 Brand Marketing Disruptor

If the first half of 2021 belongs to new consumpt...

Why did Xiaomi make a 3,000 yuan router?

On the afternoon of June 10, Xiaomi held a press ...

Is iPhone the most secure and reliable smartphone?

Is the iPhone the safest and most reliable phone? ...

Compromise or follow the trend? Why did Sony launch Android TV smart TV?

Recently, it was reported that Sony may launch HD...

How to design Tabs? I summarized these ten methods

Tabs are also called tabs (hereinafter referred t...

Ye Tan Finance "2021 Tan Tan Bull and Bear Exchange"

Ye Tan Finance "2021 Tan Tan Bull and Bear E...

4 traffic depressions for user growth in 2021!

As the title suggests, let me give the conclusion...