Android Best Practices

Source: Internet
Author: User

More than two years of Android development, but its own code is not very flattering. So I had to improve my Android program, in order to make it more solid, more professional. This article erudite, many points of view are Google, combined with their own experience to summarize some of the best practices of Android development, thick-fat can bo, accumulate more learning to be more progress.

The life cycle of Android Activity

The life cycle (Lifecycle) describes the entire process of creating an Android page from the beginning to the extinction. Along with the creation and disappearance of a series of objects, the business logic contained in the entire page is covered. and interaction with the user, accepting input from the user and returning output information to the user.


From its life flow chart, we can see that the life cycle of Android single activity is mainly OnCreate, OnStart, Onresume, Activity Running, OnPause, OnStop, OnDestroy. Usually a single app is composed of a lot of activity, the general situation we see the naked eye of a page is an activity, we also often click on button or back in the app, so we realize the switch between the activity.

1. OnCreate () and OnDestroy ()

How to understand OnCreate ()? Let's start with an official explanation from Google to see how the OnCreate () method works:

OnCreate (Bundle) is the where you initialize your activity. Most importantly, where you'll usually call Setcontentview (int.) with a layout resource defining your UI, and using Findvi Ewbyid (int) to retrieve the widgets in that UI so need to interact with programmatically

So the OnCreate method is primarily used to initialize our activity. In the OnCreate () method, we use Setcontentview (int) to specify the corresponding view for the current activity, and then retrieve the components in the current view through the ID by Findviewbyid (int). At this point, some basic initialization work for OnCreate () is completed, and the activity global initialization should be done in onCreate (), which will always be saved until OnDestroy () is called, which means the activity dies.

Careful observation found that the activity of the OnCreate (bundle) function will have a bundle type parameter savedinstancestate, through this meaningful name can see this parameter is mainly used to use the previously saved data.

The method corresponding to OnCreate () is OnDestroy (), which is the method that will be executed when the activity is destroyed, and when we call This.finish () to end the current activity manually, the OnDestroy () will be called back. We can do some preservation operations in this process before the program is destroyed.

2. OnStart () and OnStop ()

These two states are generally present when the user jumps from activity A to activity B, at which time activity A's onstop function is called back, and activity A is pressed into the stack and is invisible. Once the user ends activity B and enters activity A again, the Onrestart () and OnStart () functions are recalled again, and the activity is awakened again.

3. Onresume () and OnPause ()

By literally understanding the recovery and stopping of activity, a typical example is when the app is running with a sudden call or alarm so that the current activity is covered by other activity, which in this case triggers the Onresume () and OnPause () events, Of course, if the app goes through the OnStart () and OnStop () events, it will inevitably go through the onresume () and OnPause () events. The flowchart of the life cycle, as shown, is formed by a completed closed circuit.


The app's complete set of lifecycle,android ensures that the app works well on the phone and that it doesn't interfere with the process. The existence of the Android life cycle also indicates that in app development, the activity function should correspond to each other, register certain events in OnStart (), and in OnStop () should be anti-registration, such as GPS positioning in map development, You need to register in OnStart, and in OnStop (). There are also some network access callbacks similar to the situation, we need to be aware of in app development, to ensure that the app runs in the best state.

Activity functions-Avoid too many functions for activity classes

Activity acts as an Android controller that directly loads the interactive process with the user. In the development of activitiy is easy due to a large number of animation, layout and other operations become bloated, especially the business logic gradually complex, activity too large code increase maintenance costs, even the developers themselves are difficult to read the real meaning of the activity. To avoid this, we try to separate the pure UI layout and animation logic into third-party classes, making calls directly to their related UI layouts, and so on, once the activity interacts with the user. Our activity acts only as a real controller, controlling the receipt of user input and feedback output, while the intermediate logic can be fully referred to as "Presenter" calls.

Android layout multi-screen adaptation

Android has a large number of devices, many resolutions, different manufacturers customized SDK also increased the matching cost. Therefore, we should not use too absolute layout when we develop the process, but should use relative layout as far as possible. Overly-absolute layouts may perform well on the current device and may cause problems with layout confusion once running on other machines. Layout also avoid deep nesting between the view, according to your plan to choose the appropriate layout, the excessive layout will slow down the page rendering speed, and thus affect the performance of the entire app.

Use fragment instead of activity

Since Android4.0, Android has started to use fragment and remains constantly updated. In short, fragment is also a direct substitute for activity controller,fragment mainly for mobile phones and tablets, fragment on the mobile phone and the performance of the activity, But on the tablet can be expressed in the form of similar components, this form is more suitable for the large screen display of the tablet. Fragment lets us not only maintain the two sets of code on the phone and tablet at the same time, facilitating the development process.

MVC Best Practices

Similar to the MVC framework in Web development, we can also MVC the app. In particular, activity is our controller controllers, while specific statements such as reading and querying data should be handled through DataManager, similar to what we typically call Service,service not interacting directly with the interface. The user's interaction occurs with the activity, and the service provides some data-reading-related methods for the activity to invoke.


This article, referring to other people's articles, combined with their own practice experience summed up some practical experience. This is not enough, please kindly enlighten us, of course, you are welcome to exchange together.

Article reference: http://clayallsopp.com/posts/android-best-practices-tips/

Android Best Practices

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.