This article is a translation of a very good
topic from the Stack Overflow forum. Since English is not my mother tongue, I simply missed any places I did not understand, so as not to anger those who really know it. The article contains a list of tips and tricks for novice developers for Android.
1.
Do not forget to release resources after use : many resources such as database cursors are often ignored. Release them. Close resources (files, streams) after they are no longer needed.
2.
Do not use "magic" numbers : records of the form values ​​[0] are meaningless. There are elegant access methods, such as values ​​[SensorManager.DATA_X].
3.
Use the onPause () / onResume () methods to save or close all that this requires or that should not be open all the time.
4.
Create your applications that work quickly and efficiently : be sure to check out the reports of the
Google I / O 2009 conference.
5.
Add two numbers : your first stand-alone application should take two numbers and add them. It sounds very simple, but you can spend several hours so that all the markup (
hereinafter layout ), callback functions, onPause () / onResume () methods work correctly.
6.
This is Java : Most Android application development
is Java programming. Do not waste time criticizing or praising it. Just program it.
7.
Love RelativeLayout : Most beginner examples use LinearLayout, but you will soon find out that RelativeLayout is really useful.
8.
Use the “fill_parent” property for the very first RelativeLayout : the most common and difficult-to-find problem is to use the “wrap_content” property for the very first RelativeLayout and further clarify the reasons why some elements are drawn strangely.
9.
Use “empty” markup elements : you will often use empty elements in your markup to relatively position other elements. For example, you can use an empty TextField with zero height and width with the parameter “centerInParent” equal to “true” only to align other elements in the center of the screen.
10.
Use the background color for the markup : if you have some problems with the markup - try to set the background color for some objects. This will help isolate your errors faster than other utilities.
11.
Download Apps-For-Android : they contain quite a lot of useful source code. And nicely complement the standard application examples and show the use of other solutions in code. Download them using the svn co
apps-for-android.googlecode.com/svn/trunk/apps-for-android-read-only command .
12.
Download the source code : Android source code is needed to solve some problems and fill in the gaps in the documentation. Not necessarily that it was the latest version. Visit the
appropriate site to get it.
13.
Learn to look for the appropriate source code : the quickest solution for most problems is to find how a particular parameter is used in another source code. Place a copy of the sample apps, apps-for-android apps and any other source code in one directory. With the help of the command “grep -ir parameter directory_with the original_code /” or any other program you can easily find out how to use this parameter.
14.
Use Eclipse : even if you have another favorite IDE or editor that you have been using for years, use Eclipse for Android development. This is a pretty good IDE and for it there are many utilities that help the developer.
15.
Learn Eclipse :
Learn a few new tricks from this IDE every day. My favorite can be found
here and
here .
16.
Program every day : Android programming may disappoint you. Do not allow yourself to stop. Use utilities, sample applications, read articles. And write the code again.
17.
Do not use one monitor : working at a laptop in a cafe will slow down your work. You need to “decompose” the application windows on at least a couple of screens (the Eclipse window, the emulator and the documentation). Working with three monitors is even better.
18.
Format XML files : use the Source - Format command to make them look acceptable. If you want to choose the following option “Eclipse / Windows / Preferences / XML / XML Files / Editor / Formatting / Split XML”, then press Ctrl + Shift + F to format the text.
19.
Edit the XML files with a simple text editor : do not use the GUI to modify the properties of the XML tags.
20.
Think of piracy in MarketPlace : Google did not make MarketPlace a “happy place”. Applications are copied and published again, but only the names change. Do not plan to live solely on receipts from the AppStore.
21.
Use LogCat : it is often difficult to understand what went wrong. Run the application in the debugger and look at the LogCat output. If you need to colorize LogCat output, use
Colored LogCat22.
Investigate the utility
sdk directory : there are many useful utilities, such as hierarchyviewer or layoutopt. Examine them.
23.
Be sure to read the following articles :
design and performance in Android ,
best practices of user interfaces ,
best practices of “responsive” user interfaces, and
best practices of unit tests for Android applications .
24.
Do not copy samples of user interfaces from other platforms (especially from the iPhone) : I have seen many applications that try to copy the “tab bar” that many applications on the iPhone (
android and
iPhone ) have. I'm against it. It looks out of place, not natural for the platform and litters the screen. Also, it is difficult to implement, because the SDK does not help in creating this and you have to write it from scratch. Instead, use the Menu button.
25.
Intents launch : always launch intentions using a separate method and call this method appropriately.
26.
Garbage Collector : Launching the Dalvik Virtual Machine garbage collector is very “expensive.” It can easily block the user interface. If you need a smooth scaling, you should avoid creating multiple objects. It is preferable to use existing objects and not to create new ones.
Replica Island is a source code game that uses this technique. Explore its source codes. You can also analyze the creation of objects in the application on the “Allocation Tracker” tab in DDMS.
27.
Flows : the launch of a new flow is quite “expensive”. Use only one thread that performs tasks one by one or use thread pools. If necessary, use built-in classes, such as AsyncTask.
28.
Large amounts of data : if your data occupy a sufficiently large amount (more than 100Kb) - do not save them in text or XML files. Their parsing will be too expensive. Use SQLite.
29.
Eclipse MAT : Eclipse Memory Analyzer is a good utility for analyzing memory allocations. It helps you find memory leaks in your application.
30.
The onDestroy method : override the onDestroy method to “clean up after yourself”.
31.
Antivirus : if you use antivirus, then exclude the directories where the emulator is located from the scan: emulator images are usually stored in the user directory (for example, C: \ Documents and Settings \ Users.android), the Eclipse directory and the directory where the Android SDK is located . If you disable scanning these directories, the programming speed and debugging can increase significantly.