Android activity details (1)

Source: Internet
Author: User

What is activity? Activity is an application.ProgramProvides an interface for communicating with users. Each activity can display its own user interface through layout. An application usually contains many activities, one of which is called the main activity. This is displayed when the program is started for the first time, for example, many programs have a welcome interface. You can configure the androidmanifest. xml file to set the activity as the main activity.CodeCopy to the activity Tag:

 
<Intent-filter> <action Android: Name ="Android. Intent. Action. Main"/> <Category Android: Name ="Android. Intent. Category. Launcher"/> </Intent-filter>

One activity can start another activity to achieve different performance. After an activity is started, it is pushed into a stack to obtain the focus. When the user presses the back button, the current activity pops up from the stack (destroyed), and the previous activity is released to obtain the new focus. The lifecycle of the activity will be discussed later.

How to create an activity? Create an activity of your own by inheriting the parent class activity, which requires you to implement the callback method of the activity parent class. These methods are called in different states in the activity lifecycle, for example, create, pause, release, and destroy. There are two most important callback methods:

1. oncreate ()

This method is called when the activity is created. Each control should be initialized in this method by calling setcontentview (R. layout. XXX) define the layout of the method to display the user interface of the activity, where R. layout. XXX is the XML layout file of the activity.

2. onpause ()

This method is called when the user leaves the current activity. Therefore, you need to save the session between the user and the current activity in this method, for example, the value entered in edittext. When returning the current activity, the user should also display the value entered when leaving.

Of course, every time you create an activity, you must register an activity in the androidmanifest. xml file. For example, if the class name of the created activity is myactivity, add the following code to the file:

<Activity Android: Name=". Myactivity"Android: Label="@ String/app_name"> </Activity>

Note: If the package parameter of <manifest> is not the package of myactivity, enter the complete package name and Class Name (COM. XXX. XXX. myactivity) for the name above ).

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.