How to use Android Activity

Activity

An Activity is an application component that provides a screen that the user can interact with in order to complete a certain task, such as dialing a number, taking a photo, sending an email, or viewing a map. Each activity is given a window on which the user interface can be drawn. Windows usually fill the screen, but can also be smaller than the screen and float above other windows.

An application usually consists of multiple activities, which are usually loosely coupled. Typically, an activity in an application is designated the "main" activity, the activity that is presented to the user when the application is first launched. Each activity can then start another activity in order to perform different actions. Every time an activity starts, the previous activity is stopped, but the system keeps the activity on a stack ("back stack"). When a new activity starts, it is pushed to the top of the stack and takes user focus. The Back Stack follows the simple "last in, first out" principle, so when the user finishes the current activity and clicks the back button, it is popped off the stack (and destroyed), and the previous activity resumes.

When an activity stops because a new activity starts, it is notified of this state transition through the activity's lifecycle callbacks. There are a number of callbacks an activity may receive as a result of its own state change—whether the system created it, stopped it, resumed it, or destroyed it—and each callback offers you the opportunity to perform specified work appropriate to that state. For example, when stopped, your activity should release any large objects, such as network database connections. When the activity resumes, you can regain the necessary resources and resume the interrupted action. These state transitions are all part of the activity's life cycle.

Creating an Activity

To create an activity, you must create a subclass of Activity (or a subclass of a subclass of Activity). In your subclasses, you need to implement callback methods for system callbacks when the activity transitions between the various states of its lifecycle, such as when the activity is created, stopped, resumed, or destroyed. The two most important callback methods are:

[onCreate()](https://www.cnblogs.com/tekkaman/archive/2011/06/07/2074211.html)

You must implement this method. The system calls it when creating your activity. In your implementation, you should initialize the basic components of your activity. More importantly, this is where you must call setContentView() to define the activity user interface.

[onPause()](https://www.cnblogs.com/tekkaman/archive/2011/06/07/2074211.html)

The system calls this method when the user leaves your activity (though that doesn't always mean the activity is destroyed). This is usually where you should commit any changes that will exist beyond the user session (since the user may not come back).

There are a number of other lifecycle callbacks you should use in order to provide a smooth user experience, and an aborted table operation can cause your activity to be interrupted or even destroyed.

1、Implementing a user interface

An activity's user interface is provided by a hierarchy of views -- objects that inherit from the View class. Each View controls a specific rectangular area of ​​the activity window and responds to user interaction. For example, a view might be a button that initiates actions when the user touches it.

Android provides a large number of predefined views that you can use to design and compose your layouts. "Widgets" are views that provide visual (and interactive) elements to the screen, such as buttons, file fields, checkboxes, or just images. "Layouts" is a View inherited from ViewGroup, providing a special layout model for its sub-views, such as thread layout, grid layout or dependency layout. You can subclass the View and ViewGroup classes (or existing subclasses) to create your own widgets and objects and apply them to your activity layout.

The most common way is to define a layout using a view plus an XML layout file stored in your application resources. This way, you can maintain your user interface design independently of the code that defines the activity's behavior. You can set the layout as UI using setContentView(), passing the resource ID of the resource layout. However, you can also create new Views in your activity code, and create a view hierarchy by inserting new Views into a ViewGroup, and then use that layout by passing the root ViewGroup to setContentView().

Declaring the activity in the manifest

You must declare your activity in the manifest file in order for it to be accessible by the system. To declare your activity, open your manifest file and add an element as a child of the element. For example:

Using intent filters

An element can also specify various intent filters -- using the element -- in order to declare that other applications can activate it.

When you create a new application using the Android SDK tools, a stub activity is automatically created for you that includes an intent filter that declares that the activity responds to the "main" action and should be placed in the "launcher" category. Intent filter looks like this.

element specifying that this is a "main" entry point to this application. element specifies that this activity should be included in the list of system applications (in order to allow the user to start this activity).

If you want your application to be self-contained and not want other applications to activate its activities, then you don't need any other intent filters. Only one activity should have a "main" action and a "launcher" category, like in the previous example. Activities that you don't want to be accessed by other applications should have no intent filters and you can start them by displaying the intent themselves.

However, if you want your activity to respond to included intents from other applications (and your own), then you must define additional intent filters for this activity. For each type of intent you wish to respond to, you must contain, contain elements, optionally, an element and/or an element. These elements specify the types of intents that your activity can respond to.

Starting an Activity

You can start another activity with startActivity(), passing an Intent describing the Activity you wish to start. An Intent specifies either the prepared activity you wish to start or describes the action you wish to accomplish (the operating system chooses the appropriate activity for you, possibly customizing a different application). An intent can transfer a small amount of data to be used by the started activity.

Working entirely within your application, you will often need to simply start an unknown activity. You can do this by creating an intent that explicitly defines the activity you want to start, using the class name. For example, the following shows how an activity starts another activity named SignInActivity:

However, your application may wish to perform some action, such as sending an email, file message, or status update, using data from your activity. In this case, your application may not have its own activity to perform this action, so you can prompt an activity provided by another application on the device to perform your action. This is where intents are really valuable -- you can create an intent that describes an action you want to perform, and the system starts an appropriate activity from another application. If there are multiple activities that can handle this intent, the user can choose which one to execute. For example, if you wanted to allow the user to send email, you could create the following Intent:

EXTRA_EMAIL additionally adds to the intent an array of strings specifying email addresses. When a mail application responds to this intent, it reads the array of strings and places them into the corresponding fields. In this case, the email application activity starts and when the user is done, your activity resumes.

Starting an activity for a result

Sometimes, you may wish to receive a result from an activity you started. In this case, start the activity with startActivityForResult() (instead of startActivity()). Then receive the result from the subsequent activity and implement the onActiviryResult() callback function. When the subsequent activity completes, it returns a result to your onActivityResult() function via an intent.

For example, maybe you want the user to select one of their contacts, so your activity can do something with that contact. Here's how you create such an Intent and action result:

This example shows the basic logic you should use in your onActivityResult() function in order to manipulate an activity result. The first condition checks if the request was successful -- if it was, then the resultCode will be RESULT_OK -- and it is known whether the request was the response -- in this case, the requestCode matches the second parameter with startActivityForResult() parameters. There, the code manipulates the result of the activity by querying the data returned in the intent (the data parameter).

What will happen is that a ContentResolver implementation queries the content provider and returns a Cursor that allows reading the queried data.

Shut Down an Activity

You can close an activity by calling its own finish() method. You can also close a separate activity that you started earlier via finiActivity().

Note : In most cases, you should not display results from an activity using these methods. As discussed below about the activity lifecycle, the Android system manages an activity's lifecycle for you, so you don't need to create your own activity. Calling these functions is detrimental to the user experience and only if you absolutely do not want the user to return to the activity.

Guess you like

Origin blog.csdn.net/Eqiqi/article/details/129735804