显式Intent定义:对于明确指出了目标组件名称的Intent,我们称之为显式Intent。

隐式Intent定义:对于没有明确指出目标组件名称的Intent,则称之为隐式Intent。

说明:Android系统使用IntentFilter 来寻找与隐式Intent相关的对象。

详细解释:

显式Intent直接用组件的名称定义目标组件,这种方式很直接。但是由于开发人员往往并不清楚别的应用程序的组件名称,因此,显式Intent更多用于在应用程序内部传递消息。比如在某应用程序内,一个Activity启动一个Service。

隐式Intent恰恰相反,它不会用组件名称定义需要激活的目标组件,它更广泛地用于在不同应用程序之间传递消息。

在显式Intent消息中,决定目标组件的唯一要素就是组件名称,因此,如果你的Intent中已经明确定义了目标组件的名称,那么你就完全不用再定义其他Intent内容。

而对于隐式Intent则不同,由于没有明确的目标组件名称,所以必须由Android系统帮助应用程序寻找与Intent请求意图最匹配的组件。


Android系统寻找与Intent请求意图最匹配的组件具体的选择方法 是:Android将Intent的请求内容和一个叫做IntentFilter的过滤器比较,IntentFilter中包含系统中所有可能的待选组件。

如果IntentFilter中某一组件匹配隐式Intent请求的内容,那么Android就选择该组件作为该隐式Intent的目标组件。


Android如何知道应用程序能够处理某种类型的Intent请求呢?这需要应用程序在Android-Manifest.xml中声明自己所含组件的过滤器(即可以匹配哪些Intent请求)。

一个没有声明Intent-Filter的组件只能响应指明自己名字的显式Intent请求,而无法响应隐式Intent请求。

而一个声明了IntentFilter的组件既可以响应显式Intent请求,也可以响应隐式Intent请求。在通过和 IntentFilter比较来解析隐式Intent请求时,Android将以下三个因素作为选择的参考标准。

Action

Data

Category

而Extra和Flag在解析收到Intent时是并不起作用的。


下面通过示例来理解intent的显示和隐式:

显示:Intent intent=new Intent(MainActivity.this,test1.class);(这也是我们最常用的跳转方式)

除以上的显示跳转外,还有:(这些都是显示的调用,并跳转)

intent.setClass(MainActivity.this, test1.class);
intent.setClassName(MainActivity.this, "com.topcsa.test_android.test1");
intent.setClassName(getPackageName(), "com.topcsa.test_android.test1");
intent.setComponent(new ComponentName(MainActivity.this,test1.class));


隐式:

需要在清单文件中配置需要被调用的Activity,需通过intent过滤器设置(action、category、data),至少设置action和category:

<action android:name="com.topcsa.test_android"/>和<category android:name="android.intent.category.LAUNCHER"/>

然后在java代码上调用:

Intent intent=new Intent();
intent.setAction("com.topcsa.test_android");
startActivity(intent);//该方法里面默认的添加了一个类别(category)


当然,直接调用系统的Activity不需要在清单文件中配置。如:调用拨打电话的界面

Intent intent=new Intent(Intent.ACTION_DIAL);
startActivity(intent);





我们先用一个例子回顾一下Intent最简单的传值:

main.xml:

Android Intent的使用_bundle

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击1" />

<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击2" />

</LinearLayout>

Android Intent的使用_bundle

MainActivity.java:

Android Intent的使用_bundle

public class MainActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(getApplicationContext(), test2.class);
intent.putExtra("name", "001");
startActivity(intent);
}
});
Button btn2=(Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(getApplicationContext(), test2.class);
Bundle bundle = new Bundle();
bundle.putString("name", "123456");
intent.putExtras(bundle);
startActivity(intent);
}
});

}

}

Android Intent的使用_bundle

test2.java:

Android Intent的使用_bundle

public class test2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
TextView tv = (TextView) findViewById(R.id.ItemText);
tv.setText(getIntent().getExtras().getString("name") != null ? getIntent()
.getExtras().getString("name") : getIntent().getStringExtra(
"name"));
}

}

Android Intent的使用_bundle

从以上的例子可以看出,分为两种传值,一种是直接使用Intent,另一种是使用bundle封装后再使用Intent。

那么这两种有什么区别呢?

个人感觉没有本质上的区别。bundle更适合当传递的数据较多、传递的Acticity较多时使用,这样便可以省去在多个Activity之间传值时反复存值和取值的过程。


用intent传递list或对象

方法一: 

如果单纯的传递List<String> 或者List<Integer>的话 就可以直接使用 


Java代码 

intent.putStringArrayListExtra(name, value)  

intent.putIntegerArrayListExtra(name, value)  


方法二:

如果传递的是List<Object>,可以把list强转成Serializable类型,然后通过 

Java代码  putExtras(key, (Serializable)list)  

方法传递过去,接受的时候用 

Java代码  (List<YourObject>) getIntent().getSerializable(key)  

就可以接受到List<YourObject>数据了 

但是 切记 你的YourObject类必须要实现Serializable接口 


详细介绍一下intent传递list或对象:

Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象。

要求被传递的对象必须实现上述2种接口中的一种才能通过Intent直接传递。

Intent中传递这2种对象的方法:

Bundle.putSerializable(Key,Object);  //实现Serializable接口的对象

Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象


以下以最常用的Serializable方式为例 :

假设由登录界面(Login)跳转到主界面(MainActivity)传递的对象为登录的用户信息 User类

首先创建一个序列化类:User

Android Intent的使用_bundle

import java.io.Serializable;

public class User implements Serializable {
  private int ID;
private String UserName;
private String PWD;
public final void setID(int value)
{
ID = value;
}
public final int getID()
{
return ID;
}
public final void setUserName(String value)
{
UserName = value;
}
public final String getUserName()
{
return UserName;
}
public final void setPWD(String value)
{
PWD = value;
}

public final String getPWD()
{
return PWD;
}

}

Android Intent的使用_bundle

登录窗体登录后传递内容

Intent intent = new Intent();
intent.setClass(Login.this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("user", user);
intent.putExtras(bundle);
this.startActivity(intent);


接收端

Intent intent = this.getIntent(); 
user=(User)intent.getSerializableExtra("user");


 以上就可以实现对象的传递。

补充:

如果传递的是List<Object>,可以把list强转成Serializable类型,而且object类型也必须实现了Serializable接口

Intent.putExtras(key, (Serializable)list)


接收

(List<YourObject>)getIntent().getSerializable(key)



startActivityForResult的使用

有时,我们一个Activity可能包含多个按钮,每个按钮跳转至不同的Activity处理不同的业务,这些跳转后的Activity关闭后又要返回跳转前的Activity并返回数据怎么办?

难道又startActivity()?这时,我们有一个更好的方法,那就是使用除了startActivity()跳转的另一个跳转方法:startActivityForResult(Intent intent, int requestCode)。

但此方法需要l两个方法的结合:重写跳转前Activity的onActivityResult(int requestCode, int resultCode, Intent data)方法,设置跳转后的setResult(int resultCode,Intent data);需要说明的是这三个参数requestCode请求码、resultCode结果码、data意图是相互对应的。下面我们通过一个例子来简单的认识一下:

首先是三个布局文件:

main.xml,test1.xml,test2.xml分别如下:

Android Intent的使用_bundle

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转test1" />
<Button android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转test2" />

</LinearLayout>

Android Intent的使用_bundleAndroid Intent的使用_bundle

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/test1_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这里是test1,点击返回" />

</LinearLayout>

Android Intent的使用_bundleAndroid Intent的使用_bundle

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/test2_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这里是test2,点击返回" />

</LinearLayout>

Android Intent的使用_bundle

下面是java代码部分:

分别是MainActivity.java,test1.java,test2.java

Android Intent的使用_bundle

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
startActivityForResult(new Intent(MainActivity.this,
test1.class), 1);

}
});
btn2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
startActivityForResult(new Intent(MainActivity.this,
test2.class), 2);

}
});
}

/**
* requestCode请求码,为了知道该方法是由哪个请求的结果所触发 。resultCode结果码,为了知道返回的数据来自哪个新的Activity
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String str=data.getExtras().getString("001");
System.out.println(str);
switch (requestCode) {
case 1:
System.out.println("返回请求码为1");
break;
case 2:
System.out.println("返回请求码为2");
break;

default:
System.out.println("请求码default");

break;
}
switch (resultCode) {
case 3:
System.out.println("返回结果码为3");
break;
case 4:
System.out.println("返回结果码为4");
break;
default:
System.out.println("结果码为default");
break;
}
}

}

Android Intent的使用_bundleAndroid Intent的使用_bundle

public class test1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1);
Button btn = (Button) findViewById(R.id.test1_btn);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// 数据是使用Intent返回
Intent intent = new Intent(); // 把返回数据存入Intent
intent.putExtra("001", "scetop1"); // 设置返回数据
test1.this.setResult(3, intent); // 关闭Activity
test1.this.finish();

}
});
}

}

Android Intent的使用_bundleAndroid Intent的使用_bundle

public class test2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
Button btn = (Button) findViewById(R.id.test2_btn);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// 数据是使用Intent返回
Intent intent = new Intent(); // 把返回数据存入Intent
intent.putExtra("001", "scetop2"); // 设置返回数据
test2.this.setResult(4, intent); // 关闭Activity
test2.this.finish();

}
});
}

}

Android Intent的使用_bundle

运行后:点击跳转test1,然后点击返回按钮,接着点击跳转test2,最后点击返回,其运行结果如下:

Android Intent的使用_xml_21

从结果来理解它的运行应该够清楚了吧……


显式Intent定义:对于明确指出了目标组件名称的Intent,我们称之为显式Intent。

隐式Intent定义:对于没有明确指出目标组件名称的Intent,则称之为隐式Intent。

说明:Android系统使用IntentFilter 来寻找与隐式Intent相关的对象。

详细解释:

显式Intent直接用组件的名称定义目标组件,这种方式很直接。但是由于开发人员往往并不清楚别的应用程序的组件名称,因此,显式Intent更多用于在应用程序内部传递消息。比如在某应用程序内,一个Activity启动一个Service。

隐式Intent恰恰相反,它不会用组件名称定义需要激活的目标组件,它更广泛地用于在不同应用程序之间传递消息。

在显式Intent消息中,决定目标组件的唯一要素就是组件名称,因此,如果你的Intent中已经明确定义了目标组件的名称,那么你就完全不用再定义其他Intent内容。

而对于隐式Intent则不同,由于没有明确的目标组件名称,所以必须由Android系统帮助应用程序寻找与Intent请求意图最匹配的组件。


Android系统寻找与Intent请求意图最匹配的组件具体的选择方法 是:Android将Intent的请求内容和一个叫做IntentFilter的过滤器比较,IntentFilter中包含系统中所有可能的待选组件。

如果IntentFilter中某一组件匹配隐式Intent请求的内容,那么Android就选择该组件作为该隐式Intent的目标组件。


Android如何知道应用程序能够处理某种类型的Intent请求呢?这需要应用程序在Android-Manifest.xml中声明自己所含组件的过滤器(即可以匹配哪些Intent请求)。

一个没有声明Intent-Filter的组件只能响应指明自己名字的显式Intent请求,而无法响应隐式Intent请求。

而一个声明了IntentFilter的组件既可以响应显式Intent请求,也可以响应隐式Intent请求。在通过和 IntentFilter比较来解析隐式Intent请求时,Android将以下三个因素作为选择的参考标准。

Action

Data

Category

而Extra和Flag在解析收到Intent时是并不起作用的。

下面通过示例来理解intent的显示和隐式:

显示:Intent intent=new Intent(MainActivity.this,test1.class);(这也是我们最常用的跳转方式)

除以上的显示跳转外,还有:(这些都是显示的调用,并跳转)

intent.setClass(MainActivity.this, test1.class);
intent.setClassName(MainActivity.this, "com.topcsa.test_android.test1");
intent.setClassName(getPackageName(), "com.topcsa.test_android.test1");
intent.setComponent(new ComponentName(MainActivity.this,test1.class));


隐式:

需要在清单文件中配置需要被调用的Activity,需通过intent过滤器设置(action、category、data),至少设置action和category:

<action android:name="com.topcsa.test_android"/>和<category android:name="android.intent.category.LAUNCHER"/>

然后在java代码上调用:

Intent intent=new Intent();
intent.setAction("com.topcsa.test_android");
startActivity(intent);//该方法里面默认的添加了一个类别(category)


当然,直接调用系统的Activity不需要在清单文件中配置。如:调用拨打电话的界面

Intent intent=new Intent(Intent.ACTION_DIAL);
startActivity(intent);





我们先用一个例子回顾一下Intent最简单的传值:

main.xml:

Android Intent的使用_bundle

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击1" />

<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击2" />

</LinearLayout>

Android Intent的使用_bundle

MainActivity.java:

Android Intent的使用_bundle

public class MainActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(getApplicationContext(), test2.class);
intent.putExtra("name", "001");
startActivity(intent);
}
});
Button btn2=(Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(getApplicationContext(), test2.class);
Bundle bundle = new Bundle();
bundle.putString("name", "123456");
intent.putExtras(bundle);
startActivity(intent);
}
});

}

}

Android Intent的使用_bundle

test2.java:

Android Intent的使用_bundle

public class test2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
TextView tv = (TextView) findViewById(R.id.ItemText);
tv.setText(getIntent().getExtras().getString("name") != null ? getIntent()
.getExtras().getString("name") : getIntent().getStringExtra(
"name"));
}

}

Android Intent的使用_bundle

从以上的例子可以看出,分为两种传值,一种是直接使用Intent,另一种是使用bundle封装后再使用Intent。

那么这两种有什么区别呢?

个人感觉没有本质上的区别。bundle更适合当传递的数据较多、传递的Acticity较多时使用,这样便可以省去在多个Activity之间传值时反复存值和取值的过程。


用intent传递list或对象

方法一: 

如果单纯的传递List<String> 或者List<Integer>的话 就可以直接使用 


Java代码 

intent.putStringArrayListExtra(name, value)  

intent.putIntegerArrayListExtra(name, value)  


方法二:

如果传递的是List<Object>,可以把list强转成Serializable类型,然后通过 

Java代码  putExtras(key, (Serializable)list)  

方法传递过去,接受的时候用 

Java代码  (List<YourObject>) getIntent().getSerializable(key)  

就可以接受到List<YourObject>数据了 

但是 切记 你的YourObject类必须要实现Serializable接口 


详细介绍一下intent传递list或对象:

Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象。

要求被传递的对象必须实现上述2种接口中的一种才能通过Intent直接传递。

Intent中传递这2种对象的方法:

Bundle.putSerializable(Key,Object);  //实现Serializable接口的对象

Bundle.putParcelable(Key, Object); //实现Parcelable接口的对象


以下以最常用的Serializable方式为例 :

假设由登录界面(Login)跳转到主界面(MainActivity)传递的对象为登录的用户信息 User类

首先创建一个序列化类:User

Android Intent的使用_bundle

import java.io.Serializable;

public class User implements Serializable {
  private int ID;
private String UserName;
private String PWD;
public final void setID(int value)
{
ID = value;
}
public final int getID()
{
return ID;
}
public final void setUserName(String value)
{
UserName = value;
}
public final String getUserName()
{
return UserName;
}
public final void setPWD(String value)
{
PWD = value;
}

public final String getPWD()
{
return PWD;
}

}

Android Intent的使用_bundle

登录窗体登录后传递内容

Intent intent = new Intent();
intent.setClass(Login.this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("user", user);
intent.putExtras(bundle);
this.startActivity(intent);


接收端

Intent intent = this.getIntent(); 
user=(User)intent.getSerializableExtra("user");


 以上就可以实现对象的传递。

补充:

如果传递的是List<Object>,可以把list强转成Serializable类型,而且object类型也必须实现了Serializable接口

Intent.putExtras(key, (Serializable)list)


接收

(List<YourObject>)getIntent().getSerializable(key)



startActivityForResult的使用

有时,我们一个Activity可能包含多个按钮,每个按钮跳转至不同的Activity处理不同的业务,这些跳转后的Activity关闭后又要返回跳转前的Activity并返回数据怎么办?

难道又startActivity()?这时,我们有一个更好的方法,那就是使用除了startActivity()跳转的另一个跳转方法:startActivityForResult(Intent intent, int requestCode)。

但此方法需要l两个方法的结合:重写跳转前Activity的onActivityResult(int requestCode, int resultCode, Intent data)方法,设置跳转后的setResult(int resultCode,Intent data);需要说明的是这三个参数requestCode请求码、resultCode结果码、data意图是相互对应的。下面我们通过一个例子来简单的认识一下:

首先是三个布局文件:

main.xml,test1.xml,test2.xml分别如下:

Android Intent的使用_bundle

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转test1" />
<Button android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转test2" />

</LinearLayout>

Android Intent的使用_bundleAndroid Intent的使用_bundle

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/test1_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这里是test1,点击返回" />

</LinearLayout>

Android Intent的使用_bundleAndroid Intent的使用_bundle

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/test2_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这里是test2,点击返回" />

</LinearLayout>

Android Intent的使用_bundle

下面是java代码部分:

分别是MainActivity.java,test1.java,test2.java

Android Intent的使用_bundle

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
startActivityForResult(new Intent(MainActivity.this,
test1.class), 1);

}
});
btn2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
startActivityForResult(new Intent(MainActivity.this,
test2.class), 2);

}
});
}

/**
* requestCode请求码,为了知道该方法是由哪个请求的结果所触发 。resultCode结果码,为了知道返回的数据来自哪个新的Activity
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String str=data.getExtras().getString("001");
System.out.println(str);
switch (requestCode) {
case 1:
System.out.println("返回请求码为1");
break;
case 2:
System.out.println("返回请求码为2");
break;

default:
System.out.println("请求码default");

break;
}
switch (resultCode) {
case 3:
System.out.println("返回结果码为3");
break;
case 4:
System.out.println("返回结果码为4");
break;
default:
System.out.println("结果码为default");
break;
}
}

}

Android Intent的使用_bundleAndroid Intent的使用_bundle

public class test1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1);
Button btn = (Button) findViewById(R.id.test1_btn);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// 数据是使用Intent返回
Intent intent = new Intent(); // 把返回数据存入Intent
intent.putExtra("001", "scetop1"); // 设置返回数据
test1.this.setResult(3, intent); // 关闭Activity
test1.this.finish();

}
});
}

}

Android Intent的使用_bundleAndroid Intent的使用_bundle

public class test2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
Button btn = (Button) findViewById(R.id.test2_btn);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// 数据是使用Intent返回
Intent intent = new Intent(); // 把返回数据存入Intent
intent.putExtra("001", "scetop2"); // 设置返回数据
test2.this.setResult(4, intent); // 关闭Activity
test2.this.finish();

}
});
}

}

Android Intent的使用_bundle

运行后:点击跳转test1,然后点击返回按钮,接着点击跳转test2,最后点击返回,其运行结果如下:

Android Intent的使用_xml_21

从结果来理解它的运行应该够清楚了吧……