What pits should be noticed in Android development

Source: Internet
Author: User

As a two or three years of Android application development experience of the code farm, Nature will encounter a lot of pits, the following is I can think of some pits (practice proves not to take notes is not a good habit), behind the thought of other pits will be mended in succession.

1. You cannot use the Switch-case statement to access the resource ID in the Android Library: Reason analysis and solutions for accessing resource IDs in the Android library without using the Switch-case statement

2. Cannot display Popupwindow and dialog:popupwindow-problems creating a Popup Window in Android activity when activity is not fully displayed

3. Do not share data with sharedpreferences between multiple processes, although it can be (mode_multi_process), but extremely unstable: android-mode_multi_process for sharedpreferences Isn ' t working

4. Sometimes you can not use the application context, otherwise it will be an error (such as Start activity, show dialog, etc.):

5. The same application of JNI code, do not easily change the NDK compiled version, otherwise there will be a lot of problems (mainly some methods to achieve different, and higher version of the code to detect more stringent), such as R8 no problem, but to R9 there is a problem, this is a big hole;

In the JNI code of 6.Android, there is no error when a function with return type does not compile with the return value;

7. The OnPause method of the current activity will not execute the next activity after the execution of the OnCreate method, so the OnPause method is not suitable for long-time work, which will affect the efficiency of the jump between pages;

8. Use Android's transparent theme with caution, and transparent themes can cause many problems, such as: if the new activity uses a transparent theme, the current activity's OnStop method is not invoked, and when you press the home key in the activity interface set to transparent theme, May result in the brush screen is not clean problem, into the theme of the transparent theme of the interface will have a noticeable delay sense;

9. Do not initialize the viewstub in a non-UI thread, otherwise it will return null;

10. The public interface must take into account the code re-entry situation, can be designed as a single case to try to use a single case;

11. Do not pass large chunks of data through the bundle, otherwise the transactiontoolargeexception exception will be reported: java-issue:passing large data to second Activity

12. Try not to cache data through application, which is not stable: do not cache data in Android application objects!

13. Try not to use animationdrawable, it will be initialized at the time of all the pictures loaded into memory, especially memory, and can not be released, after the release of the next step into the load will be error;

14.9 figure can not be compressed through tinypng, otherwise there will be problems;

15.genymotion Simulator is fast because it is based on the x86 architecture, if you use so in your application, but there is no x86 architecture so, can only abandon the use of it; The emulator for Android studio is the same;

16.Eclipse Android Development environment configuration is not easy to upgrade ADT and build tools, otherwise you will waste a lot of time, there is a workspace in the project not too much, or every start will be very slow;

17.Android Studio Each version, gradle each version of the difference is relatively large (I think), for JNI code compilation recommendations in Eclipse, if the development of JNI in Android Studio will waste a lot of time, The main is the configuration of the compilation script is more troublesome;

18.Eclipse in the lint is too unreliable, especially when the main project relies on the library, many tips are problematic, recommend the use of Android Studio engineering cleaning tools, especially recommended;

19. Different API version of the Asynctask implementation is not the same, some can perform multiple tasks at the same time, some APIs can only execute one thread at a time, Therefore, when executing multiple asynctask in a program, it is possible to encounter a Asynctask Excute method that has not been executed for a long time. Calling Asynctask's Excute method does not immediately execute the program's cause analysis and improvement scheme

20. In the same application, the same images are placed in drawable-xxhdpi, drawable-xhdpi, drawable-hdpi, drawable-mdpi, drawable-ldpi, and the memory used in the same device is very different ( The DPI of the device is fixed, the picture is placed in a different DPI folder, the picture needs to be converted to the same DPI as the current screen and displayed on the device, so even if the picture is the same size in different DPI folders, but the size in memory is not the same, it is not necessarily a long * width Before you do the memory optimization of your application, you can take a look at how your project is adapted to the screen and whether there is room for optimization. This screen is highly recommended for video tutorials and can be read in 2.5 hours: android-screen to fit all tips

21. Care for database upgrades (such as the need to add fields in the original database), to avoid data loss or operation of database anomalies, the database upgrade method can be consulted in the first line of code P263;

22. Multiple programs share a set of code (a set of code, on the desktop more than one icon) need to handle the different entry when the stack problem;

23. When using adapter, if you use Viewholder to do the cache, in GetView method regardless of whether each view of this item needs to set properties (such as the TextView set property 2 may be null,item the background of a button is transparent, The color of an item is transparent, etc.), you need to set the properties for all the views of each item (the TextView property is empty and you need to set the SetText (""), the background transparency also needs to be set), otherwise the content will appear garbled during the sliding process.

24. Careful use of Android multi-process, although the multi-process can reduce the memory pressure of the main process, but will encounter the following problems: (1) can not fully exit all activity function (if there is a peer in the application of multiple processes successfully implemented a complete exit program welcome communication);(2) The first time you enter the page of the new startup process there will be a delay phenomenon (it is possible to black screen, white screen a few seconds, is the white screen or black screen and the theme of the new activity);(3) when the process is applied, the new START process will run again application OnCreate method, Do not re-create a application, but will re-run application OnCreate, so that the data can not be cached in the application as a way to share memory; (4) Multi-process through sharedpreferences sharing data is not stable, specifically can be consulted "Android development art exploration."

25. When using toast, it is recommended that you define a global toast object, which avoids the case where the last toast message cannot be canceled when the toast is continuously displayed (if you have a continuous popup toast, avoid using toast.maketext);

The larger the 26.View area is, the longer the drawing time, and the transparent channel has great influence on the drawing speed of the view;

27. Do not pass large objects through MSG, which can cause memory problems.

What pits should be noticed in Android development

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.