Android activity usage

Source: Internet
Author: User
In Android development, activity is very important. In an application, every screen displayed is an activity. therefore, to learn Android, you must have a certain understanding of the activity. there are also some introductions about activity in other forums. Here I want to talk about some of my views on activity learning. first, the lifecycle of an activity is very important. The activity mainly contains six methods: oncreate, onstart, onresume, onpause, onstop, and ondestory.

Oncreate corresponds to ondestory. onstart corresponds to onstop, and onresume corresponds to onpause.

These functions are defined in this way. When an activity is started, the oncreate method is started first, followed by onstart and onresume. Generally, after these functions are started, your activity will be displayed. of course, I am talking about the general situation here. If you start a backend service during the initialization of the three functions, you have to wait until the serviceconnection execution is complete before it can be displayed, someone may ask what a service is. I will introduce it later. Here, we will first understand that the Service callback function will also affect the startup of acitivity. this is the three functions that will be called when the activity is started. onpause, onstop, and ondestory will be called when the acitivity is destroyed. after ondestory is called, your acitivity will be destroyed. At this time, when you call the isfinishing of the activity, true will be returned, however, the this pointer of the activity can be used at this time. If you start a single thread in the activity to do other things, the context pointer of the context variable can still be used.

Of course, the activity may be in another State, not necessarily displayed or destroyed. It is very likely that the activity starts another activity, at this time, the previous Activiy will be put into the system stack. When the started activity returns, it will be displayed again. This process is like this, if an activity starts another activity, it will call the onpause function to enter a stagnant state. After the started activity is destroyed and returned, it will call the onresume function again. for this mechanism of activity, I feel that it is best to put the initialization work in an interface defined by myself, because the activity status changes, the layout of your activity may be changed. here we will talk about layout. The layout of each activity's interface is a layout. Every activity must have such a layout to be displayed. Generally, we will put a layout in an XML file, and then directly call the setcontentview function of the activity to fill the activity. If layout is not placed in an XML file, you can also use Code Generate a dynamic layout, that is, use activity. this pointer generates a layout. we recommend a very useful tool called droiddraw. There is a link in the Forum, and the webmaster seems to have posted a post. this tool is very easy to use. You don't have to read the tutorial for half an hour. There are some specific controls on it. After setting the control layout, you can directly generate xml files. put this XML file under the Res/layout folder of the project. after the layout file is generated, you need Program You can set the ID of some controls you want to use on the Google Android homepage, gettingstarted. I won't repeat it here. next, let's talk about the main functions of Android. I also suggest writing as little code as possible in the oncreate function, put everything as much as possible in onresume and the custom initialization function. The probability of onresume being called is very high, here, you may not feel the importance of onresume during simulator development. When testing on a real machine, you will find that the screen becomes black and enters the waiting state, then, when you manually restore the screen to light up, it will also enter the onresume status, so I feel that it is necessary to put some methods of refreshing the control into the onresume function. onpause, onstop, and ondestroy functions are used for cleaning. For example, some variables are to be released, and some threads are to be stopped.

Below I will summarize a general design idea of activity:

First, design a method. The main function of this method is to initialize the activity control, perform various conditions judgment, and initialize the la s that are not needed for acitvity, here is an example to explain why different la s need to be initialized. For example, if your activity reads some information from the SD card, the information will certainly disappear when you pull out the SD card, then you need to execute another layout to display this activity. At this time, you can reuse this function to initialize the layout.

Oncreate function: registers the variables you want to use, such as service and consumer er. These variables can be responded to no matter whether your activity is in the foreground or in the background, then, call the previously initialized function to initialize the layout information.

Onstart function: registers some variables. These variables can be responded only when the activity is in the foreground.

Onresume function: Call some functions to refresh the UI. Every time the activity calls this function, it will refresh the status of each UI control.

Onpause function: Generally, some variables are set, because the activity will be immediately switched to the background for processing at this time, and some variables may be released or the status should be adjusted accordingly.

Onstop function: the variable registered in the onstart function.

Ondestory function: a variable registered in the oncreate function.

I talked about some of the most commonly used methods of activity. Of course there are still many methods that I haven't talked about. I have never used many methods. For other methods, refer to the Google documentation.

Next, let's talk about some of the simplest communication methods in the activity. Here I first define two names to facilitate the subsequent description. The activity that starts another activity is called the main activity, the started activity is called a child activity.

There are many ways to communicate between the main activity and the sub-acitivity. Here we will introduce the two simplest methods.

Method 1: parameters are transmitted through intent. There are various putxxx methods in intent to store various parameters. Then, when the child activity receives this intent, it can retrieve this parameter from this intent, use getintnet (). you can use the getxxxextra () method.

Method 2: When a main activity wants to receive messages from a child activity, it can use the startactivityforresult method. For example, to start an activity, startactivityforresult (I, request_code ); then, the onactivityresult method in the main activity determines the requestcode to process different responses to different child activities. In addition, the child activity can also use the setresult method to set the resultcode in the main activity method, in this way, the main activity can handle different situations based on different resultcode of the sub-activity.

Let's write so much about it today. If you have any questions, please add QQ.

I am adding: Now Android seems to have very few Chinese materials. Generally, I want to learn from Google's English documents,

if you have a deep understanding of Java, I think Android applications are easy to use.

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.