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 ).