Activity configuration, startup, and shutdown activity instances detailed _android

Source: Internet
Author: User

Let's look at the effect chart:

Android provides us with four types of components, activity, Service, broadcast receivers, and content providers, which are the cornerstones of our development of an Android application. The system can be entered into the development application through the pointcuts provided by different builds. Not all builds are practical pointcuts for users, but they are interdependent, each of which acts as a specific role as an entity of existence, helping developers define the behavior of Android apps as a unique cornerstone. Below I will organize my own activity learning bit:

A acitvity as a user interface that is displayed on the screen, such as in an e-mail application, an activity that displays a list of items, an activity to write a message, an activity to read the content of a message, and so on. Activity is used to provide a user experience, and many different experiences of the activity gathered together to form an Android application user experience, each activity is independent of each other. Applications can also access other apps ' acitivity (which need to be allowed by app), in addition to having access to their own activity.

1. How do I create an activity?

You must create a subclass of the activity in which you need to implement the functions of the system callback (OnCreate, OnStart, Onresume, OnPause, OnStop, OnDestroy) that the activity state switches through the lifecycle. Of course, not all functions need to be implemented again. Two of the more important functions are oncreate and


OnCreate (), this method must be overridden. The system calls this method to create an activity, which is an important step in initializing the activity you created. One of the most important is to call Setcontentview () to define the layout of the user interface you want to show.

OnPause () calls this method when a system task user leaves the interface, not destroying an activity at this time. This is usually where you deal with changes that persist beyond the user's session, such as data preservation.

To ensure a smooth user experience and processing, you can invoke other callback functions to stop or destroy your atctivity. In the OnStop () method, the release of some large resource objects is generally done, such as: network or database connection. You can reload the required resources at onresume time.

2 Creating an Activity

public class Mainactivity extends the activity { 
//must override method 
protected void OnCreate (Bundle Savedinstancestate) { 
super.oncreate (savedinstancestate); 
Setcontentview (r.layout.activity_main);//activity layout 

2. After an activity is created, in order for it to be accessible the system must be declared to register it into the application's Androidmanifest.xml file:

android:name= "com.zy.demo.activity.MainActivity" 
android:label= "@string/app_name" > 
<action android:name= "Android.intent.action.MAIN"/> 
<category Android:name= "Android.intent.category.LAUNCHER"/> 

<activity> has many attributes for developers to define activity with different characteristics, such as lable, icon or theme, style, and so on. Where Android:name is a required attribute that defines the name of the activity and cannot be changed when the application is released.

<activity> also provides a variety of intent-filter, using <intent-filter> to declare how other application components are activated (started) activity,<intent-filter> contains <action> and <category> two elements. As in the above example <action android:name= "Android.intent.action.MAIN"/> Used to indicate that this activity needs to respond to Android.intent.action.MAIN (indicated as the main portal for the application), <category android:name= " Android.intent.category.LAUNCHER "/> Indicates that the activity is a LAUNCHER category, where the application is listed in LAUNCHER and allows the user to start directly. The above is also an application of the main activity must be declared method: a main action, and a launcher category. If you want the activity to respond to the implicit intent of other applications, you need to have the action declared for the activity, and you can add categor and data.

Start of 3.Activity

3.1 startactivity

By invoking the StartActivity (intent) Start activity,intent is used to accurately describe the activity you want to start, or the action,intent you want to perform can also be used to carry small data to be started acitivity.

When in the middle of the same application need to simply start another activity,intent explicit definition you want to start the activity class can:

Define a Intent, named Activity:tostartactivity Intent to start 
Intent = new Intent (mainactivity.this,tostartactivity.class ); 
Use StartActivity () to start the activity 

When your application needs to perform actions that it does not have activities to perform, we can use the activity of other applications on the phone instead of execution. such as sending a mail, viewing a picture, searching for a word, and so on. This is the important point of intent, you can define a intent describe the behavior you want to do, when you send to the system, the system will start the appropriate acitivty to help you perform, if there are more than one application activity can handle this behavior, the system will allow users to select one. Once this activity has been executed, the original activity will be more than

Cross-application search from Google interface 
Intent Intent = new Intent (intent.action_web_search); 
Intent.putextra (Searchmanager.query, "Zy"); 

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

Intent Intent = new Intent (); 
Specifies that you want to start the complete package name for the build, the object name 
componentname cn = new ComponentName ("", 
" "); 
Intent.setcomponent (CN); 
A new task 
intent.addflags (intent.flag_activity_new_task) is required to use context.startactivity (); 
StartActivity (Intent);

3.2 Startactivityforresult

Receives the results of the initiated acitivity feedback by calling Startactivityforresult (intent). In order to receive the results of the activity that starts next, you need to rewrite the Onactivityresult () callback function. When the calling activity completes, it returns a intent containing the result to onactivityresult () processing. For example, in an application activity, a user is required to select one of the contacts, and the activity needs to get some information about the contact person:

 Intent Intent = new Intent (Intent.action_pick, Contacts.People.CONTENT_URI); 
Initiates an activity Startactivityforresult (intent, Pick_contact_request) with the returned result of the selected contact;
Here's the pick_contact_request for the custom int type request feedback result code. Re-onactivityresult () is used to process received return results @Override protected void Onactivityresult (int requestcode, int resultcode, Intent da  TA) {//If the request Requestcode successfully and the result returned by the request ResultCode is the pick_contact_request if (ResultCode = = Activity.result_ok && Requestcode = = pick_contact_request) {//process intent returned data, find the name of the contact in the contact database Cursor Cursor = Getcontentresolver (). Query (DAT 
A.getdata (), new string[] {Contacts.People.NAME}, NULL, NULL, NULL); 
if (Cursor.movetofirst ()) {//If cursor is not empty, find out the contact's name int columnindex = Cursor.getcolumnindex (Contacts.People.NAME); 
String name = cursor.getstring (columnindex); Add additional Features}}} 

Here in order to describe the use of onactivityresult () to handle the return results, the first thing to check is whether the request succeeds, and then whether there is a return result, whether the result is startactivityforresult (), if satisfied, The data returned through the intent is processed.

4. Closure of activity

1 The activity can call the finish () method to close itself, or you can turn off an independent, previously initiated activity by calling Finishactivity ().

2 Call Finishactivity () method to close a standalone activity that started before

This method is used to turn off an activity that is enabled using Startactivityforresult (Requestcode) 

About when to close an activity, generally by the system directly for our management. But when you confirm that the user does not have to return to the activity, we call the above method to close the corresponding activity.

5 Demo Code:

Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View; public class Testuseaactivity extends appcompatactivity {@Override protected void onCreate (Bundle savedinstancestate) {s
Uper.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_test_use_a); //Start the first activity public void startfirstactivity (view view) {Intent Intent = new Intent (this, testuseaactivity2.class); s
Tartactivity (Intent);
//Start the second activity public void startsecondactivity (view view) {Intent Intent = new Intent (this, testuseaactivity3.class);
StartActivity (Intent); //Start a third activity, which is closed after 4 seconds. public void startthirdactivity (view view) {Intent Intent = new Intent (This, Testusea
Startactivityforresult (Intent, 1);  New Thread () {@Override public void run () {try {thread.sleep (4000); finishactivity (1);} catch (Interruptedexception e)
{E.printstacktrace ();}}
}.start (); }

The above is a small set to introduce the activity configuration, start and close the activity examples, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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: 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.