The activity and intent of Android

Source: Internet
Author: User
Tags event listener

An Android application typically has more than one activity that communicates with each other and transmits data or jumps from one activity to another. This allows an application to have a rich functional and diverse interface.

Intent objects are required for activity to pass data and to jump to each other. Intent has many uses in the Android program. We are now focused primarily on the role it plays in communicating between the two activity. Simply put, intent is the carrier of data and operation instructions between the activity provided by the Android object. Intent is responsible for the operation of the application of the action, action involved in data, additional data description, Android according to this intent description, responsible for finding the corresponding components, the intent passed to the calling component, and complete the component calls. A Intent object contains a set of information, consisting mainly of the following information:

1.Componetname 指定Intent的的目标组件的类名称(即指定要启动的 Activity的名称)
2.Action 指定要启动的组件要执行什么样的操作
3.Data 执行动作要操作的数据
4.Category 被执行动作的附加信息
5.Extras 其它所有附加信息的集合,主要是一些键值对,在一个Activity中 定义,通过Intent传递到另一个Activity中
6.flags 标志指示Android系统如何去启动一个Activity

The following examples illustrate how to use the intent object in the midst of two activity.

The main process of the program is: there is a button in Activity01, add a click event listener for this button, and when we click the button, the click event is triggered. The code in the event rushes to our top a intent object that specifies componentname through the Setintent method of the intent object, and passes a string in Activity01 that has a key value of "passed string arguments" to Otheractivity. The otheractivity is then started and the string parameter passed in the TextView control in Otheractivity is displayed.

Create a new project activity_intent. Create an Activity object named Activity01 in the project. Writing XML code in a Activity01 layout file makes Activity01 only one button.

Activity01.java

01.package org.idea.android;
02.import android.app.Activity;
03.import Android.content.Context;
04.import android.content.Intent;
05.import Android.os.Bundle;
06.import Android.view.View;
07.import Android.widget.Button;
08.import Android.widget.TextView;
09.import Android.view.View.OnClickListener;    10.public class Activity01 extends activity {11. /** called the activity is a.    * * 12.    @Override 13.        public void OnCreate (Bundle savedinstancestate) {14.
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Button button= (button) Findviewbyid (R.id.mybutton);
Button.settext ("Click to jump to another activity");     Button.setonclicklistener (New Buttononclicklistener ())//Register Click event Listener 19 for the button.    } 20.        Class Buttononclicklistener implements Onclicklistener {21.        @Override 22.            public void OnClick (View v) {23.            TODO auto-generated Method Stub 24. Intent intent=new Intent ();//Definition IntentObject 25.            Intent.setclass (Activity01.this,otheractivity.class);//Specify ComponentName 26.            Intent.putextra ("Value", "string parameter passed");//Pass key value to 27.        Activity01.this.startActivity (Intent)/Start other activity 28.    } 29. } 30.}

Main.xml

01.<?xml version= "1.0" encoding= "Utf-8"?> 02.<linearlayout xmlns:android=
"http://schemas.android.com" /apk/res/android "
.    android:orientation= "Vertical"
.    Android:layout_width= "Fill_parent"
.    android:layout_height= "Fill_parent"
.    >
07.<button
.    Android:id= "@+id/mybutton"
.    Android:layout_width= "Fill_parent"
.    android:layout_height= "Wrap_content" one by one
.    />
12.</linearlayout>

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.