Android Core Foundation fifth day

Source: Internet
Author: User

I. Learning objectives and REQUIREMENTS

Course Objectives

Course content (what must be said is the order of the knowledge points of the lecture)

* Mastering activity Configuration

* Mastering Intent Display

* Mastering Intent Implicit Intent

* Use scenarios with two intentions

* Mastering activity-enabled data transfer

p> * Mastering the life cycle of activity

* Mastering the lifecycle of the screen toggle

* Mastering the nuances of the different Android versions life cycle

* Mastering start activity Get return value

* Mastering the function of the request code result code

* Mastering the program entry activity configuration parameters

* Mastering display     opening new activity

* Mastering Implicit intent     Implicit intent to open a new activity    implicit intent intent filter

* Mastering two intent Usage scenarios

* Mastering activity-enabled data passing

Passing basic type data

Passing Object data

* Mastering the life cycle of activity

* Mastering the life cycle     life cycle control of the screen switch

* Mastering different Android versions  & nbsp;  life cycle nuances

* Mastering activity Get return value

Case: Select Contact message mass Sender

* Mastering the function of the request code result code

Distinguish the type of request from the type of return

/td>
Ii. Learning Essentials 1.☆activity Parameter Configuration 1.1 application entry activity needs to configure two parameters

<!--program entry activity needs to be configured with two parameters--
<intent-filter>
<!--Current activity is the application's entry--
<action android:name= "Android.intent.action.MAIN"/>
<!--Current Activity creates a shortcut to the desktop app--
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>

1.2 An application can create multiple portals of activity, shortcut icons

<!--as long as it's configured, a new shortcut icon is created on the desktop--
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>

1.3 Configuration in the Activity manifest file
Property Role Description Example
Action Activity corresponding to the action Can be described in any one string (preferably, as the name implies) <action android:name= "Android.intent.action.MAIN"/>
Category Additional category Parameters Designated working devices (Car pc, set-top box, default phone) <category android:name= "Android.intent.category.CAR_DOCK" >
Data

Specifying the type of data and data

Scheme data prefix
Host hostname
Port Port number
The path path is preceded by A/
MimeType Data types
<data android:scheme= "Calc"/>
The important role of 1.4 activity

Decoupling between the > Applications.
You can call each other without an application's interface.
The > application is open, not closed.

2.☆☆intent Intent 2.1 Intent Explicit intent

An explicit intent. Specify the activity that you want to activate directly. High efficiency is directly specified.

An explicit intent
The first form of a notation
Intent.setclassname (This, "com.itheima.rpcalc.SecondActivity");
StartActivity (Intent);
The second type of notation
Intent Intent = new Intent (this,secondactivity.class);
StartActivity (Intent);

2.2 Intent Implicit Intent

Action, data, category) to open a new activity.
Primary scenario: Activating activity for another application. Query the system to meet the conditions of activity, and then activate activity, low efficiency, relatively slow.

//Implicit intent
Intent.setaction ("Com.itheima.rpcalc.view");
Intent.addcategory ("Android.intent.category.DEFAULT");
Intent.setdata (Uri.parse ("calc://Zhang San"));
StartActivity (Intent);
2.3 Usage scenarios for two intents

If activation is an activity within the application, it is recommended to use explicit, directly specifying the package name Class name. High efficiency.
If the activity of another application is activated, it is recommended to use the implicit intent, specified by action and data.

3.☆intent Passing Data 3.1 large basic type data and arrays of data can be passed through intnet

Intent.putextra (name,values)

3.2 Intent passing objects

The requirement object must be a serializable object that implements the serializable interface.
The requirement object must be to implement Parcelable interface

Javase serialization is the object that serializes the file inside.
Parcelable serialized into memory.

3.3 Passing a set of data

Bundle Extras = new bundle ();
Intent.putextras (extras);

4.☆ opening a new activity gets the return value 4.1 when the caller activity opens a new interface

Startactivityforresult (Intent, 0);

4.2 In the new open activity if you want to set the return value.

Setting result data
Setresult (0, data);


4.3 Newly opened activity if you have finished, close
finish ();

4.4 Data is returned to the caller (opener) activity
onactivityresult (int requestcode, int resultcode, Intent data) {}
The data parameter is the returned result. Remove the data from the inside.
Requestcode Request Code Distinguish which button the request is from
ResultCode Result Code Differentiate which result the request is from
5.☆☆activity life cycle

5.1 What is life cycle

Human life cycle---------High School---> University---------------------

Java code. The life cycle of an object
The activity lifecycle object is taken from new to the method that is executed during the Dailvik virtual machine collection.

5.2 Important methods of life cycle

Called when the OnCreate-->activity is created. Suitable for the initialization of the interface.
Called when the OnDestroy-->activity is destroyed. Suitable for the closure of the operation. The operation of the data save. Saving SMS Content

OnStart--->activity interface user visible. Suitable for updating the interface. Continue playing video
OnStop--->activity interface user is not visible. The interface is not visible for cleanup operations. Pause the video. Video Player

Onresume-Get Focus button can be clicked
OnPause--The Lose focus button can be seen, but cannot be clicked. Game Use
5.3 Common Life cycles

full life cycle oncreate-->onstart-->onresume-->onpause-->onstop-->ondestory

visual life cycle Onstart-->onresume-->onpause->onstop

If the interface is not visible OnStop method, the next interface re-visible, first call Onrestart (), and then call OnStart ();

foreground life cycle Onresume-->onpause

5.4 Life cycle of screen switching

By default, the toggle activity is destroyed--re-created
Turn the screen toward death
android:screenorientation= "Landscape" horizontal screen
android:screenorientation= "Portrait" vertical Screen
Setting the screen to toggle is not sensitive
Android:configchanges= "Orientation|keyboardhidden|screensize"

6 processes, threads, applications, task stacks, startup mode 6.1 processes

Android in progress
Android system, application exit and process exit are two different concepts.
Android system in order to let the application can be quickly opened. After all applications are exited, the process does not exit.
The process is recycled only if the system has a serious shortage of memory space.
Click the icon-->linux Create the process-->dalvik the virtual machine and read the manifest file, load activity
Android understands app exit: task Stack emptied.

6.2 Threads

A process is the unit in which the operating system allocates memory space, and the memory space for each process is independent.
The thread is running inside the process. The smallest unit of thread CPU execution. If the process hangs, the thread also hangs.

6.3 Application Application

Android apps each app is running in its own sandbox (sandbox).
Understood as a set of activity,service,content Priovder broadcastreceiver combinations.

6.4 Details of the task stack

Each application runs, and the system allocates a new task stack.
Question: How many task stacks exist in the system?
How many applications are running (no activity is destroyed, back to the desktop), there are several task stacks.
The ID of the task stack is an integer of type int, self-growing ID.

6.5 Startup mode

There are four modes of startup

Standard start-up mode. An activity default is the standard startup mode. Opening a new activity,activity will be created and added to the stack top of the task stack. Suitable for most application scenarios.

Singletop single top boot mode. If found, the activity has been started, and at the top of the stack, the activity will not be recreated.
Solve a user experience problem. Avoid opening the interface repeatedly that has been opened multiple times.
for (int i = 0;i<100;i++) {
Pop-up Favorites
}
Browser added to bookmarks, singletop
There can be multiple instances in the same task stack.

Singletask single task stack startup mode. When you start a new activity, you check to see if there is an instance of the activity in the task stack, and if there are instances, simply empty all the activityon the instance and reuse the existing activity instance.
Only one instance exists in the same task stack.
When do I use the Singletask mode?
Browseractivity Browser overhead (memory footprint, CPU consumption) is very large, singletask. Ensure that only one instance exists in a task stack.
WebKit the kernel page.
HTML parser
HTML renderer
CSS Renderer
JavaScript execution engine
If the resource overhead of an activity is very large, it is recommended to use the Singletask startup mode.
The browser's activity uses the Singletask startup mode.

SingleInstance a single instance. Similar to the singleton mode in Java, the single-state mode. There is only one instance of activity in the entire Android phone operating system.
The activity in the SingleInstance startup mode runs in its own separate task stack.
> If you want an activity, there is only one instance of the entire mobile operating system. Configure SingleInstance.
The activity configuration mode for >incallscreen calls is singleinstance. There is only one instance of him in the entire mobile OS.

6. Important examples

6.1 Character Calculator 6.2 SMS transmitter

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.