Android Survival Guide: Development Considerations _android

Source: Internet
Author: User
Tags java se
1. Declaring system configuration change events for the activity
System Configuration Change event refers to the turn screen, regional language changes, screen size changes, and so on, if the activity does not declare handling these events, the event occurs, the system will kill and then restart the activity, and try to restore state, The activity has the opportunity to save some basic data through the onsaveinstancestate () to the bundle, and the bundle will pass through the OnCreate () of the activity. While this may seem normal, it can cause problems because many other things, such as dialog, depend on specific activity instances. So the default behavior of this system is usually not what we want.
To avoid these system default behaviors, it is necessary to declare these configurations for the activity, as follows two of which each activity must declare:
<activity android:configchanges= "Orientation|keyboardhidden" >
Almost all activity has to be stated as above, why does Android not turn them into default?
2. Use the Android API as much as possible
It's kind of a nonsense to develop on Android without using the Android API. Because Android almost supports all of the Java SE's APIs, there are many places where the Android API and Java SE APIs overlap, for example, for files that are best done using the API in the context package in Android, Instead of using the file object directly:
Context.openfileoutput (String); No file File = new file (String)
The reason is that the API will take into account the characteristics of the Android platform itself, such as, less thread, and more use of asynctask and so on.
3. Taking into account the killing of activity and process
If you usually quit the activity, there are also activity for other reasons, such as low system memory, system configuration changes, anomalies, etc., to consider and test this situation, especially when the activity processing important data, good data preservation.
4. Be careful with multiple languages
Some languages are really long-winded, Chinese or English is very short to express things to other languages will become dead long, so if it is wrap_content may be the other control extrusion visual range; If you specify a length, you may not be able to display the whole. Also pay attention to special languages such as those that read from right to left.
5. Do not use the four components to implement the interface
First, the components of the object are relatively large, the implementation of the interface is more wasteful, and make the code more difficult to read and understand, but also more important is to cause a multi-party reference, may cause a memory leak.
6. Use Getapplication () to take the context as a parameter
For a function that needs to use the context object as a parameter, use Getapplication () to get the context object as a parameter, rather than using this, unless you need a specific component instance! The context returned by Getapplication () is application, it will exist throughout the life cycle of the application, much larger than the lifecycle of a component, so even a reference holding a context object for a long time does not cause a memory leak.
7. The main thread only does UI control and frameworks callback related things. The subordinate threads do only time-consuming background operations. The interaction only passes through the handler. This prevents a large number of threading problems.
8. Frameworks callback do not do too much to do the necessary initialization, other things that are not very important can be done in other threads, or use handler schedule to do it later.
9. To consider multiple resolution
At least for hdpi, MDPI, ldpi to prepare pictures and layouts. The unit of the element also uses the dip as much as possible instead of PX.
10. Use the Android phone's hard button
Almost all Android phones have back and menu, and their function is to return and pop-up menus, so do not design the Back button and menu buttons in the UI. Many good applications, such as notes and micro-letters have return keys, because they are all ported from iOS, in order to save the consistency of the experience, so there are returns and menus. But this is not enough to Android, a pure Android is no need to repeat the hard key function.
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.