📜 ⬆️ ⬇️

6 things I would like to know before developing my first Android application

From the translator : the translation was done immediately, in one sitting, so that in some places it can be a bit clumsy. In any case, you know where to write about errors.
There are some sensible thoughts in the comments to the original article, such as not using libraries from the world of “big” java, as they are too voluminous, or using any Glide-s instead of Picasso (with which, however, I agree). You can see if interested.
And yet, I didn’t figure out how to make snippets appear from the github, so I just copied the code.

My first application was terrible. In fact, it was so nightmarish that I deleted it from the market and will not even indicate it in my resume anymore. That application might not be so terrible if I knew a few things about Android development before writing it.

The following is a list of things you need to understand while developing your first Android application. These lessons were learned from the real mistakes that I made in the source code of my first program — mistakes that I will give below. Understanding these things will help you write an application that you will be proud of a little more.
')
Of course, if you write something in the course of learning Android-development, most likely, you still hate your creation anyway. As @codestandards said:

If you’re not learning enough. - Code Standards (@codestandards) May 21, 2015

(If the code you wrote a year ago doesn't seem bad to you, you probably haven't studied enough)

If you are an experienced Java developer, points 1, 2 and 5 may not be of interest to you. On the other hand, items 3 and 4 can show you some cool things you can do with Android Studio that you haven't heard about, even if you didn't make the mistakes given in the examples for these items.

1. Do not create static context references.



public class MainActivity extends LocationManagingActivity implements ActionBar.OnNavigationListener, GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener { //... private static MeTrackerStore mMeTrackerStore; //... @Override protected void onCreate(Bundle savedInstanceState) { //... mMeTrackerStore = new MeTrackerStore(this); } } 


This may look like a mistake that no one would have thought of. This is not true. I thought of it. I saw people who made the same mistake, and I interviewed people who could not immediately explain why this was a serious mistake. Do not do this. This is noobie.

If MeTrackerStore retains a link to the Activity passed to the designer, this Activity will never be destroyed by the garbage collector (until this static link is reassigned to another Activity). This is because mMeTrackerStore is static, and the memory for static variables is not released until the process in which the application is running ends. If you are tempted to do so, apparently, something seriously wrong with your code. Seek help. Maybe Google's Udacity course will help you: " Android Development for Beginners ".

Note: technically, you can store a link to the application context without a memory leak, but I still do not recommend it .

2. Beware of “implicit references” to objects whose life cycle you do not control.



 public class DefineGeofenceFragment extends Fragment { public class GetLatAndLongAndUpdateMapCameraAsyncTask extends AsyncTask<String, Void, LatLng> { @Override protected LatLng doInBackground(String... params) { //... try { //Here we make the http request for the place search suggestions httpResponse = httpClient.execute(httpPost); HttpEntity entity = httpResponse.getEntity(); inputStream = entity.getContent(); //.. } } } } 


There are several problems with this code. I will focus on one. In Java, (nonstatic) inner classes have an implicit reference to an instance of the class that contains them.

In this example, any GetLatAndLongAndUpdateMapCameraAsyncTask would have a reference to the DefineGeofenceFragment that contains it. The same is true of anonymous classes.

GetLatAndLongAndUpdateMapCameraAsyncTask has an implicit link to the fragment, an object, and we do not control the life cycle. Responsibility for creating and destroying fragments lies with the Android SDK, and if GetLatAndLongAndUpdateCameraAsyncTask cannot be cleared by the collector because it is still running, its implicit reference to the DefineGeofenceFragment prevents the collector from deleting the fragment.

Here's a great Google I / O video that explains why things like this happen .

3. Make Android Studio work for you



 public ViewPager getmViewPager() { return mViewPager; } 


This snippet contains what you get when using “Generate Getter” in Android Studio. Getter leaves the “m” prefix in the generated method name. This is not the best option.

(If you are more concerned about why the prefix “m” is added to an instance variable: “m” is often added according to code-convention. It stands for “member”)

Regardless of whether you think that adding a prefix is ​​a good idea, you should know that Android Studio allows you to write code according to any convention that you follow. For example, you can use the code style settings to have Android Studio automatically add a prefix to your instance variables and delete it when generating getters, setters, and design options.

image

In general, Android Studio can do much more. Studying shortcuts and templates is where to start.

4. Methods must do one thing.



In one of the classes I wrote there was a method longer than 100 lines. Such methods are difficult to read, modify, and reuse. Try writing methods that do just one thing. As a rule, all methods longer than 20 lines should be under suspicion. In the search for such methods, you can ask for help from Android Studio.

image

5. Learn from people who are smarter and more experienced than you.



It may sound trivial, but I made this mistake when writing the first application.

When you create programs, you will be mistaken. Other people have already made these mistakes. Learn from these people. Repeating problems solved by others, you are wasting your time. I spent a lot of time on my first application, making mistakes that I could have avoided if I had just spent a little more time learning from experienced developers.

Read the Pragmatic Programmer . Then Effective Java . These two books will help you to avoid the basic mistakes that beginners make. Having finished with these books, keep looking for smart people from whom you can learn.

6. Use libraries



When you write your application, you are likely to encounter problems that more intelligent and experienced people have already solved. Moreover, many of these solutions are available as open source libraries. Take advantage of them.

In my first application, I wrote code that provided the functionality already provided by the libraries. Some of the libraries were standard. Others are third-party, like Retrofit and Picasso. If you are not sure which libraries to use, you should do the following three things:

  1. Listen to the Google I / O Fragmented podcast series. In this series of developers they ask which third-party libraries they consider important when developing for Android. Spoiler: These are mainly Dagger, Retrofit, Picasso, and Mockito.
  2. Sign up for Android Weekly . They have a section containing the latest libraries that have appeared. Watch him, there you can find something useful.
  3. Look for open source applications that solve problems similar to those that you encounter in your application. There you can see that the developer used a third-party library, which you did not find, or a standard Java library, which you did not know about.


Conclusion



Writing good Android programs can be very difficult. Do not complicate your life even more by repeating my mistakes. If you find an error in what I wrote, let us know in the comments (erroneous comments are not worse than their absence). If you think this will be useful for some newcomer, share the article with him. Save them from some headache.

Source: https://habr.com/ru/post/263079/


All Articles