Four basic Android components: androidactivity
Recently, I have prepared a system to review all aspects of Android. Based on the knowledge sharing principle, I will record some things in the process and use it as a learning note.
Next, let's start with the question .....
What is Activity?
Activity is one of the four major Android components and one of the most important components. As a component that interacts with users, we can compare Activity to a folder window on windows, which is an interface that interacts with users. Further, when we pick up Android and turn on the dial-up function, we can see that the dial-up interface is actually an Activity. Of course, it can be said to a wider extent, every page displayed on the mobile phone screen is an Activity.
Activity Status
Running status: When the Activity is at the front of the screen, Actiivity is fully displayed on the user's interface, and the focus is obtained, the Activity can respond to events such as the user's touch screen, which is running.
Paused: When the Activity is blocked by other activities, but some are still visible, the Activity is paused. When the Activity is paused, the UI is updated, but the focus cannot be obtained at this time, that is, it does not respond to user touch, back and other events.
Stopped: When the Activity is completely blocked by another Activity, it is in the stopped state. The stopped State does not mean that the Activity stops running, but can be understood as being transferred to the background for running. However, if the system requires memory, the stopped Activity is terminated first to release resources. Therefore, important data must be saved when the Activity is transferred to the stopped status.
Activity Lifecycle
PS: when the system memory is insufficient, the system can release the Activity to obtain resources. The Activity in the stopped state is preferentially released, followed by the Activity in the temporary state, however, in the running state, Acitivity cannot be forcibly released unless it is absolutely lacking in resources (this means it will crash directly ...), When an Activity is released, it is directly transferred to the dead state. Note that onDestroy () is not executed, so it is inappropriate to store the data in onDestroy for processing. You can go to onSaveInstanceState () process important data accordingly.
Start Activity
When we want to jump from the current Activity to the next Activity, we need to know how to start the Activity. refer to the following code:
Intent intent = new Intent (this, OtherActivity. class); // write method 1 // Intent intent = new Intent (getApplicationContext (), OtherActivity. class); // write method 2 // Intent intent = new Intent (); // write method 3 // intent. setClass (this, OtherActivity. class); startActivity (intent); // start another Activity
Intent is the bond between the four main components. When an Activity is started, it is generally a new Intent instance, and the corresponding startup component, such as OtherActivity. class, is specified. Call the Activity method startActivity (intent) to notify the android system to load the OtherActivity.
Data transmission between activities
For an application, simply starting another Activity from one Activity is far from enough. Sometimes it is necessary to compare data and information exchange. The android system provides many data communication methods, and naturally provides a simple data transmission method for Activity communication. See the following code:
FirstActivity transmits data to OtherActivity <data, "transmitted data">
1 Intent intent = new Intent (this, OtherActivity. class); 2 3 intent. putExtra ("data", "transmitted data"); // data is key4 // another method for loading data 5 // Bundle bundle = new Bundle (); 6 // bundle. putString ("data", "transmitted data"); 7 // intent. putExtras (bundle); 8 9 startActivity (intent );
There are two Representation Methods. Generally, the first method is used to transmit a small amount of simple data. The latter method is used to transmit a large amount of complicated data. However, at the underlying layer, the first method is the encapsulation of the latter method. It still creates a bundle in the intent and stores the data (for example, "transmitted data" in the example ") put it in.
OtherActivity obtains transmitted data <data, "transmitted data">
1 Intent intent = getIntent (); 2 // method 3 String data = intent. getStringExtra ("data"); // the value of data is "transmitted data", which is passed into 4 by the previous Activity // another method 5 // Bundle bundle = intent. getExtras (); 6 // String data = bundle. getString ("data ");
Generally, the get method first obtains the bundle carried by the intent, and then obtains the data stored in it through the get method. The previous method does not explicitly obtain the bundle code, because the intent. the getStringExtra (key) function obtains the bundle and parses the data of the corresponding key. Therefore, the two methods are essentially the same, however, we recommend that you use the latter method if you use a large amount of data, because getting data from bundle is more efficient than getting data from intent. In the mobile age, efficiency is very important ..
FristActivity starts OtherActivity and obtains the data returned by OtherActivity at the end of the activity.
If the business needs to start another Activity and return data to the original Activity, should startActivityForResult (intent, requestCode) be called instead of startActivity (intent ), requestCode is an identifier code for the Activity to be started and a positive integer value, when multiple started activities exist, different identifiers can be used to indicate the data returned by the Activity and process it differently. In FristActivity, the returned data is processed in onActivityResult (requestCode, resultCode, data), and the first parameter requestCode is the ID code entered at startup. See Code
1 Intent intent = new Intent (this, OtherActivity. class); 2 startActivityForResult (intent, 0); // you can specify 0 as the identifier.
This is the code to start the Activity. However, to return data, we also need to rewrite onActivityResult (requestCode, resultCode, data) in FristActivity)
1 @ Override2 protected void onActivityResult (int requestCode, int resultCode, Intent data) {3 super. onActivityResult (requestCode, resultCode, data); 4 if (requestCode = 0) {// which Activity returns bundle5 Bundle bundle = data according to the value of requestCode. getExtras (); 6 String getdata = bundle. getString ("name"); 7} 8}
Then, rewrite the method for returning data in the started OtherActivity.
Intent intent = new Intent (); intent. putExtra ("name", "returned data"); setResult (1, intent); // you can specify the intent finish () for the returned data. // you can call this operation to end the current activity.
Generally, after setResult, you need to call finish () to end the current Activity. In this case, the data will be returned to FristActivity. The data returned is called before onActivityResult is called.
Activity ends
Finish (): After the execution of finish (), the system will remove the activity from the stack, but the resource is not released immediately, and the onDestroy life cycle method will not be called.
OnDestroy (): When the activity is executed to this lifecycle, it means that the activity will be completely released. If you need to restart this activity, you need to re-onCreate ().
System. exit (0): This will kill the entire Application and exit the process.
What are four components of android?
Android has four components: Activity, Service, Broadcast explorer, and Content Provider.
The communication between these components is mainly completed with Intent assistance.
Intent describes the actions, actions involving data, and additional data of an application. Android identifies the corresponding components based on the description of the Intent, pass Intent to the called component and complete the call of the component.
Therefore, Intent acts as a media intermediary here, providing information about component calls to each other to decouple callers from callers.
For example, in an application maintained by a contact, when we click a contact on a contact list screen (assuming the corresponding Activity is listActivity, you want to jump out of the contact's detail screen (assuming the corresponding Activity is detailActivity)
To achieve this purpose, listActivity needs to construct an Intent, which is used to tell the system that we want to perform the "View" action. The corresponding viewing object of this action is "a contact ", call startActivity (Intent intent ),
The constructed Intent is passed in. The system will find the Activity that meets the Intent requirements in ManiFest according to the description in this Intent. The system will call the Activity that is found, that is, detailActivity, and finally pass in the Intent, detailActivity performs the corresponding operation based on the description in the Intent.
If you agree with my answer, click the "select as satisfactory answer" button below. Thank you!
What are the four main components of Android? How do you understand them?
Android has four components: Activity, Service, Broadcast explorer, and Content Provider.
Activity
It is really difficult to build a complete Android program and don't want to use Activity, unless you want to be crazy. Because Activity is a window for interaction between Android apps and users, in my opinion, from this perspective, Android Activity is a webpage of a website.
Activity is undoubtedly the most complex among the four main components. During the past few years, a hook between something and the interface cannot be simplified. Think about it, you can figure out how much time it takes to create an independent application on the interface. In terms of visual effects, an Activity occupies the current window, responds to all window events, and has interface elements such as controls and menus. From the internal logic point of view, the Activity needs to do a lot of persistent things to maintain the status of each interface. It also needs to properly manage the lifecycle and some switch logic. For developers, they need to derive a subclass of the Activity, and then work hard to do the above. For more details about the Activity, see reference/android/app/Activity.html. In the future, we will provide a more detailed analysis.
Service
Service, in the most straightforward view, is to strip the Activity of the interface. They are very similar in many Android concepts and are encapsulated with a complete functional logic implementation, however, the Service is not exposed, but it is a solid backing silently.
But in fact, from another perspective, the services in Android are similar to the Windows Services and Web Background services that we usually call. These services are usually used for long running of the background and accept upper-layer commands, the module that completes related transactions. In running mode, Activity jumps from one to the other ..., this is a bit like a modal dialog box (or a web page ...), give an input (or no ...), then, regardless of whether you want it to run, the output (same as or without...) is returned when it leaves ...).
The Service is not, it is waiting, waiting for the upper layer to connect to it, and then generate a persistent and lingering communication, which is like an Ajax page, watching nothing changed, sneaky and Service, I don't know how many times it went.
However, unlike general services, the process model of Android services is configurable like all four components, both the caller and the producer can choose whether to run the component in the same process or in different processes. This sentence can be engraved into your mind, which highlights the running characteristics of Android. If a Service is expected to run in different processes of the caller, you need to use the RPC mechanism provided by Android to deploy a set of inter-process communication policies for it.
Shows the RPC implementation of Android, as shown in (well, it also comes from the SDK ...), nothing unusual. Based on an implementation of the proxy mode, a proxy class is generated on both the caller and the server side for serialization and deserialization, this allows both the caller and server to use the RPC interface just like calling a local interface.
The class used for data serialization in Android is Parcel. For details, see/reference/android/OS/Parcel.html. This class encapsulates the serialization details and provides an access interface that is sufficiently object-oriented, android is said to be very efficient.
There is also the Android Interface Definition Language (AIDL), an Interface-defined Language, and the RPC Interface of the service, which can be described by AIDL, the ADT can help you automatically generate a complete set of classes required for the proxy mode, which is the kind of kind that is hard to write. For more information, see guide & #47 ...... the remaining full text>