Activity configuring, starting and closing activity instances

Let's look at the renderings first:

Android provides us with four application components, namely activity, service, broadcast receivers and content providers. These components are the cornerstone of our development of an Android application. The system can enter the developed application through the entry points provided by different components. For users, not all components are practical entry points, but they are interdependent. Each of them, as an existing entity, plays a specific role and serves as a unique cornerstone to help developers define the behavior of Android applications. Next, I will sort out my activity learning tips:

An acitvity acts as a user interaction interface displayed on the screen. For example, in an e-mail application, an activity used to display the recipient list, an activity used to write e-mail, an activity used to read e-mail content, and so on. Activities are used to provide user experience. Many activities with different experiences can form the user experience of an Android application. Each activity is independent of each other. In addition to accessing their own activities, applications can also access the acitivity of other apps (which need to be allowed by the app).

1. How to create an activity?

You must create a subclass of activity. In the subclass, you need to implement the system callback functions (oncreate, OnStart, onresume, onpause, onstop, ondestroy) when the activity state is switched in the life cycle. Of course, not all functions need to be re implemented. Two of the more important functions are oncreate and

onPause:

Oncreate(), this method must be overridden. The system calls this method to create an activity. Implementing this method is an important step for you to initialize the activity you created. The most important thing is to call setcontentview () to define the layout of your user interface.

Onpause(), this method will be called when the system task user leaves this interface, instead of destroying an activity. Usually, we have to deal with some persistent changes beyond the user session, such as data storage.

In order to ensure smooth user experience and processing, you can call other callback functions to stop or destroy your atactivity. In the onstop () method, some large resource objects are generally released, such as network or database connection. You can reload the required resources when onresume.

2 create activity

2. After an activity is created, in order for it to access the system, it must declare that it is registered with the application's androidmanifest In the XML file:

< activity > there are many attributes for developers to define activities with different characteristics, such as label, icon, theme, style, etc. Android: name is a required attribute, which is used to define the name of the activity. It cannot be changed after the application is published.

< activity > also provides various intent filters. Use < intent Filter > to declare how other application components activate (start) activity. < intent Filter > contains two elements < action > and < category >. In the above example, < action Android: name = "Android. Intent. Action. Main" / > is used to indicate that this activity needs to respond to Android intent. action. Main (indicates that it is the main entry of the application), < category Android: name = "Android. Intent. Category. Launcher" / > indicates that the activity is the launcher category, that is, the application will be listed in the launcher, allowing the user to start directly. The above is also the declaration method required for the main activity of an application: a main action and a launcher category. If you want the activity to respond to the implicit intent of other applications, you need to declare the corresponding action for the activity, and you can also add category and data.

3. Activation of activity

3.1 startActivity

Start the activity by calling startactivity (intent). Intent is used to accurately describe the activity you want to start or the action you want to perform. Intent can also be used to carry small data to the started activity.

When you need to simply start another activity in the same application, intent clearly defines that you want to start the activity class:

When your application needs to perform some actions that it does not have an activity to perform, we can use the activities of other applications on the phone instead. For example, send an email, view a picture, search for a word, and so on. This is the important point of intent. You can define an intent to describe the behavior you want to do. After you send it to the system, the system will start the appropriate activty to help you execute it. If there are multiple application activities that can handle this behavior, the system will let the user choose one. When this activity is executed, the original activity will be

When starting an activity across applications, you must specify a specific acitvity for the intent when defining it, provided that the activity must be exposed outside its own application (Android: exported = "true"):

3.2 startActivityForResult

By calling startactivityforresult (intent), you can receive the feedback result of the activated activity. In order to receive the results of the next activity, you need to override the callback function onactivityresult(). When the called activity completes, it will return an intent containing the result to onactivityresult() for processing. For example, in the activity of the application, the user needs to select one of the contacts, and the activity needs to get part of the contact information:

Here, we want to explain that onactivityresult() is used to process the returned result. First, check whether the request is successful, then whether there is a returned result, and whether the result is required in startactivityforresult(). If it is satisfied, process the data returned through intent.

4. Close the activity

1 an activity can call the finish () method to close itself, or it can call the finishactivity () method to close an independent previously started activity.

2. Call the method of finishactivity () to close an independent previously started activity

When to close an activity is generally managed by the system directly for us. However, when you confirm that the user does not need to return to this activity, we call the above method to close the corresponding activity.

5 demo code:

The above is a detailed explanation of the activity configuration, startup and shutdown examples introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>