Android entry-level activity

Source: Internet
Author: User
Activity Introduction

Address: http://blog.csdn.net/Vange

========================================================== ========

 

An application is composed of multiple activities. Different activities perform different functions.

The Android system's high-level applications are composed of multiple activities, each of which controls the corresponding functions, similar to the C (Controller) in MVC mode ), it is responsible for data processing and control interface selection and partial display. The coupling of the activity is very low. As long as the intent-filter is set in the app, other apps can also call its activity. When multiple calls are selected, the menu list is displayed.

The activity uses the interface to express itself. It does not necessarily display the whole screen. Sometimes it is only a small part of the screen.

Each activity uses setcontentview () to set the corresponding view to display its own data. Most of the display is displayed on the entire screen, but sometimes it is only part of the display screen, such as the progress circle of some apps during loading and dictionary query at the top of the screen.

The application has a main activity as the entry to start other activities.

The activity will have a main activity as the entry to start. Which of the following identifiers must be created in the file during creation:

<Activity Android: Name = ". exampleactivity" Android: icon = "@ drawable/app_icon">

<Intent-filter>

<Action Android: Name ="Android. Intent. Action. Main"/>

<Category Android: Name ="Android. Intent. Category. Launcher"/>

</Intent-filter>

</Activity>

Android. Intent. Action. MainIndicates that this activity is the app entry;

Android. Intent. Category. LauncherIndicates that the portal is started as a system icon or shortcut;

(The intent-filter introduction will be added later)

 

However, the app does not have only one entry, but may also start the app by enabling intent-filter.

 

Create a simple activity

1. Step 2: Create a subclass that inherits from the activity as the logic code for implementing the activity.

2. Step 2: create the following fragments in the application node in the androidmanifest. xml file. Generally, the corresponding XML has been generated by yourself when you create the fragments using IDE.

<Activity Android: Name = ". activity. Second"> </activity>

3. Step 2: call a new activity in the main activity.

Startactivity (new intent (this, second. Class ));

Generally, it is recommended that you call the activity method provided by the static method of the new activity (Second Class). During the call, you only need to pass the corresponding parameter, the new activity is used to construct intent calls, so that the logic can be put in the corresponding activity, and the code will be clearer. See the following code (from K-9 mail open source code:

Public static void actionsettings (context, account Account ){

// Assemble an intent here

Intent I = new intent (context, accountsettings. Class );

// Add the required data to the intent

I. putextra (extra_account, account. getuuid ());

// Start the corresponding activity

Context. startactivity (I );

}

 

The display sequence queue of the activity follows the stack model, and the first opened activity is on the top.

After each activity in the same app is started, the original activity will be placed in the return sequence stack of the activity (from the android website ):

1. Start the main activity. It may be started by using the System icon or intent-filter. At this time, Activity 1 is placed in the return sequence stack of the activity.

2. When you click "Start activity 2", the system pulls activity 2, and activity 2 is put into the stack;

3. When you click "Start Activity 3", the system pulls Activity 3, which is put into the stack;

4. When you click the return button, the system will exit Activity 3 and return it to Activity 2;

When the finish () activity is not put into the queue

1. The main activity is pulled up;

2. The main activity pulls Activity 1, and 2 is displayed in the stack;

3. When Activity 1 pulls activity 2 and calls the finish () method, Activity 1 will not be put into the stack, but will end the lifecycle directly, activity 2 is put into the stack. When you press the return key again, it is returned to the main activity;

 

Currently, many apps need to press the return key multiple times before exiting, and sometimes appear multiple times on the same activity interface. You can use this method to solve the problem, for example, the following code:

Public void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Custom );

Startactivity (new intent (this, second. Class ));

Finish (); // call the finish () method to end the lifecycle after the startup

}

Introduction to the lifecycle of an activity

After each app is pulled up, the activity starts its own life cycle. It may be displayed at the beginning or after other activities, sometimes it is hidden behind other activities and recycled by the system (when system resources are insufficient). Sometimes it is called by other apps until its lifecycle ends.

Every cycle calls back the corresponding code in the activity implementation class, such as oncreate () and onstart. See:

1. onresume () is a key point. You need to call this callback function whether the activity is pulled for the first time or pulled back to the front end, therefore, data and interface initialization can be processed in this method;

2. onsaveinstancestate () is used for callback when a new activity is to be pulled up. According to the function name, we can see that this callback function mainly stores some data, to prevent the original scenario from being initialized when the activity is used again;

3. The key point is that when the system resources are insufficient or the user uses some process management software to kill the app, many apps do not consider this point during development, therefore, an application is placed in the background for a period of time. When you use this app again, a program error occurs, or some variables are not initialized;

4. ondestroy () is the called callback function when an activity has completed its own lifecycle;

5. When a new activity is pulled up, onstop () is called after creation. Generally, resources are released on onstop and on onresume () confirm necessary resources;

 

 

For the order of calls, you can write a simple example of activity calling other activities. At the same time, you can write log on each callback function to see the log print as shown in the following figure:

 

Write so much first, and then add more.

Related Article

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.