Android Activity Personal Understanding

Source: Internet
Author: User

Activity is important in Android development, where each display is an activity in an app. Activity in the API on Android. Under the app package, it inherits some of the classes:
Java.lang.Object
Android.content.Context
Android.content.ContextWrapper
Android.view.ContextThemeWrapper
One person feels that the activity in Android is similar to the servlet in the web, in order for the servlet to respond to the user's needs, Need to go to the HttpServlet dorequest () and Doresponse () method in the replication program, etc. Activity is similar to creating an activity that requires many of these callback methods, the most common being the Oncreat method, which is a callback when the activity is created, One practical Setcontentview method to display the layout interface to show.

//Inherit activity  Public classMainactivityextendsActivity {//declaring the component to be used PrivateTextView Mytextview; PrivateButton MyButton; //overriding the OnCreate method  Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Set Current ViewSetcontentview (R.layout.main); }   }  


Let's talk about the configuration of activity in Androidmanifest.xml:
<activity android:name= ". Mainactivity "android:label=" @string/app_name ">
The above is the general configuration format for activity, and the configuration <intent-filter> label is required if it is the main function that starts as the first one.

 <  intent-filter  >  <  action  android:name  = "Android.intent.action.MAIN"                    />  <  category  android:name  = "Android.intent.category.LAUNCHER"               />  </ intent-filter  >  

An Android app usually contains a lot of activity, but only one activity is acting as the main interface, and the main interface's action is set to Android.intent.action.MAIN, Each activity can have only one action location, but multiple category,category can be used at the same time to display method declarations and hermit method declarations when intent initiates other activity.
Activity initiates other activity in the following two ways:
1.starActivity (Intent Intent): Start other activity
2. Startactivityforresult (Intent intent,int requestcode): Initiates activity with the requested code, after the program shuts down the activated activity, Previous activity can get the return value by means of the Onactivityresult method of replication.
All two of the above need to use the parameter intent,intent is an important part of Android is the communication set up, the person who feels familiar with the Javaweb struts2, Or someone who knows about the MVC design pattern will feel that the intent design is somewhat similar to STRUTS2 's logical view design, where intent's display declarations are as follows, and there is a hermit's statement, which is explained later in the special introduction intent.
Display Mode declaration Intent, direct start secondactivity
Intent Intent = new Intent (firstactivity.this,secondactivity.class);
The activation activity also has a 4 load mode:
1.standard mode
This mode starts the activity with a new instance every time, and adds the activity to the top of the task stack, which does not start a new task, and the activity of the heart will be placed at the top of the current task stack.
2.singletop mode
This pattern is basically similar to the standard mode, and one thing that is different is that when the activity that you want to launch is already at the top of the task's stack, it does not create the activity of the heart, but rather directly the current activity.
3.singletask mode
When the activity does not exist, the system creates an instance of the activity and the top of the holiday task in its normal form. If the activity is already at the top of the stack processing method and singletop, and finally when the activity already exists but not at the top of the stack, all the activity in this task is removed and a new activity is created at the top of the task stack
4.singlelnstance mode
This mode is relatively simple, the current activity to start does not exist then create a new task to hold the activity, if there is already a need to create activity will have to call the activity of the task to go to the foreground, thus displaying activity.
Speaking of start-up in terms of shutting down, it is much easier to shut down than to start the activity, and there are only two ways to close it:
1.finish (): Close the current activity, equivalent to this. The meaning of finish.
2.finishActivity (int Requestcode): This method corresponds to the Startactivityforresult start method above. Use the value of code to distinguish where the return value is handled.
After starting two activity, there is the problem of communicating between two activity, where intent provides many overloaded methods to provide the exchange of data:
1, Putextras and bundle Getextras, wherein the former is to intent into the need to carry the data, the latter is taken out of the data, the general will first top a list or map function to hold the packet.
2.putExtra (String name,xxx value) and Getxxxextra (Sting name), where the former is stored in the data like intent in accordance with Key-value, the latter is the value of the key to take out the corresponding data, overall , is the encapsulation of a map collection.
3.putserializable (string key,serializable data) and getserializable (string key,serializable data), where the former is a serializable The latter is to take out the corresponding serialized data. There is a detailed explanation of what is serializable to see in the Java base IO stream.
The above is the active communication between the activity, continue to see the activity of the internal callback mechanism, said the activity of the callback function is very easy to think of Javaweb inside the servlet running on the Toncat server, depending on the scene, The implementation of the choice is init,destroy,doget,dopost,service and so on, and the activity in Android is similar to the servlet in Javaweb, This means that the activity has many life-cycle related methods to invoke after it has been developed. Activity only needs to be configured in the Androidmanifest.xml, as for when the activity is instantiated, its method is appropriate to call, can make a very accurate judgment.
It says the activity's declaration cycle, which can roughly be summarized as four states: active, paused, stopped, and destroyed.
In the overall life cycle there are roughly the following methods that will be recalled by the system:
1.onCreat: Called when the activity is created.
2.onStart: Called when the activity is started.
3.onRestart: Called when the activity is restarted.
4.onResume: Reply to the activity when the call, Onrestart method must callback this method.
5.onPauser: Call when activity is paused
6.onStop: Call when activity is stopped
7onDestroy: Call when destroying activity
Just as the servlet was developed to choose a method that needed to be replicated, activity was the same way of making a copy of it according to its own needs, the most common being the Oncreat method that was called when the activity was created.
The focus of the activity in mind is the above, but it also took some time to review the servlet's feeling that there are many similarities between the two. For example, the activity and servlet need to be configured, are the base class of the system, all need to respond to the container to run, there is a life cycle of the callback method and so on some, of course, there are many differences between the two, can think of only two points: 1. The activity will eventually be displayed via Android, and the servlet will not be displayed, and the servlet can only return the corresponding JSP page through the method to the client by the other scripting language to display the page, 2.Activity through the intent to jump pass the value, The servlet mainly controls the jump through the user's request object. In my opinion, it seems that the Activity of Android also has the feeling of similar MVC pattern design, which is to do the data layer (Sope and other protocol return data) and the presentation layer (layout module) to separate design, through the middle layer (Activity) to transfer. (MVC, it is a design pattern, it is mandatory to make the application of input, processing and output separately), of course, this is a beginner's initial understanding, read a lot of Android content, the above is for the activity of their own understanding and summary, These days, when you have ideas, you will summarize the other three of the four components of Android.

--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------

Every time I write this summary will have a lot of new ideas, but the time to write this article is the longest, mainly for the servlet is a little rusty. I recalled most of the things by taking Javaweb's book and turning it around two.

Android Activity Personal understanding

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.