Activity in Java

Source: https://blog.csdn.net/lidandan2016/article/details/78030688

Activity first, activity is one of the four components in the Android system and can be used to display view. Activity is a system module that interacts with user records. Almost all activities interact with users, but it is not correct to say that activity is mainly used to display views.

Before we have an in-depth understanding of activity, we should first know the MVC design pattern. In JavaEE, the MVC design pattern is very classic and the division is relatively clear. However, in Android, many people are not very clear about the application of MVC in Android development. Let me first introduce the application of MVC in Android Development:

Invalid Sign

Well, after introducing the MVC architecture in Android application development, we can clearly know that in Android, activity is mainly used for control. It can select the view to display, or obtain data from the view, then transfer the data to the model layer for processing, and finally display the processing results.

After introducing the main functions of activity, we will talk about activity in detail. Activity lifecycle diagram

The life cycle of an activity is controlled by the following functions. public class Activity extends ApplicationContext { protected void onCreate(Bundle icicle); protected void onStart(); protected void onRestart(); protected void onResume(); protected void onFreeze(Bundle outIcicle); protected void onPause(); protected void onStop(); protected void onDestroy(); }

Oncreate (bundle) function is the place where you initialize. It is also the place where you execute setcontentview (view) function. Setcontentview (view) function can pass in a UI interface prepared by XML, which can completely separate the UI from the specific implementation. The onpause() function is where the user leaves the current activity. More importantly, any changes in the current activity must be submitted in this function.

Activity has four states: activity state. When the activity is at the top of the stack, it is the current reality screen of the mobile phone. This means that the activity is in activity or running state. Run but lose focus. When the activity is still in the running state, but another activity is in the document focus state on the screen, the current activity is in pause. Stop. When an activity is completely covered by another activity, it is stopped. In fact, although it is running, it is invisible to the user. End. When the activity is in pause or stop, the system can end the activity and recycle resources. This is the end state of the activity. The activity is in the end state. If you want to make it visible to the user, you can only restart it.

The response time of an activity. The thread where the current activity is located is the main thread, and its response time is 5 seconds. If a time-consuming operation is performed in the currently running activity and the response time exceeds 5 seconds, the program will report an anr error. Therefore, this is one of the reasons why it is not recommended to write too much complex code in an activity. Of course, some code can only be written in the activity, otherwise it will not run (they are not life cycle methods). For example, if you want to get some information about Android system or hardware, you must write it in the activity. If you write a tool class alone, you can't get it.

1. Main functions of activity

Activity is a very important user interface for Android. It is a visible interface for interaction between users and applications. Many controls can be placed in each activity, so the activity can also be regarded as a container for controls.

2. Key points of creating activity

1) An activity is a class, and the class inherits the activity, and the inherited activity comes from android.jar package. 2) the oncreate method needs to be copied. When an activity runs for the first time, the Android operating system will call the oncreate method. 3) because the activity is a component of the application, each activity must be registered in androidmanifest.xml, Manifest is equivalent to an application manifest. 4) Add the necessary controls for the activity

3. Register the method of applying activity in the androidmanifest.xml file

1) When registering an activity, add a dot in front of the name, because it is just the path of this class (or activity) combined with the package name. 2) Label is the text displayed in the white part at the top of the page. 3) intent filter, which doesn't matter for the time being, means which activity you configure this thing and which activity runs first when the application is opened

4. Adding controls to an activity is to create a layout file in the layout folder (strictly speaking, not every activity needs to create a corresponding XML layout file, such as textview textview = new textview (this);

//In the past, I always thought that a layout file must be created for each activity. Now I find that this is not the case. You can also create a simple textview according to this method, set the context to this, set a text value, and then set the textview to contentview. Personally, I think it is more convenient to test.

textview.setText("This is the Artists tab");

setContentView(textview);

}

}

This method works the same, but in general, it is recommended to define a layout file for each activity.

1) Setcontentview in the program is the layout file used to set this activity. For example: setcontentview (r.layout. My_text)// Here, please note that the name of the layout file cannot be capitalized. 2) the control ID set in the XML file will generate ID in r.java. 3) use findviewbyid in the application to obtain the control in the layout file (provided that the control in the layout file must be set with the corresponding ID), because its return type is view, So an example of forced type conversion to control type is button = (button) findviewbyid (r.id.button)———————————————— Copyright notice: This is the original article of CSDN blogger "lidandan 2016", which follows the CC 4.0 by-sa copyright agreement. Please attach the original source link and this notice for reprint. Original link: https://blog.csdn.net/lidandan2016/article/details/78030688

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>