使用    Intent 启动另一个 Activity

Intent  showNextPage_Intent=new  new  new  new  Intent();

showNextPage_Intent.setClass(UsingBundel.this    ,NextPageActivity.class);

startActivity(showNextPage_Intent);


在多个Activity 之间切换时候,注意每个  Activity 都应在 AndroidManifest.xml AndroidManifest.xml AndroidManifest.xml AndroidManifest.xml  中有所声明定义(如下)

<application  android:label="@string/app_name"

android:icon="@drawable/chinazphone" >

<activity  android:name=".UsingBundel"

android:label="@string/app_name" >

<intent-filter >

<action  android:name="android.intent.action.MAIN"  />

<category  android:name="android.intent.category.LAUNCHER"  />

</intent-filter>

</activity>

<activity  android:name=".NextPageActivity"

android:label="@string/nextpage" ></activity>

</application>

新的 Activity 在 AndroidManifest.xml 中必须定义声明



使用 Bundle  在 Activity间传递数据


从源请求 Activity 中通过一个 Intent  把一个服务请求传到目标 Activity 中

 private  Intent  toNextIntent;//Intent 成员声明

toNextIntent=new Intent();//Intent 定义

toNextIntent.setClass(TwoActivityME3.this,  SecondActivity3. class);

// 设定开启的下一个 Activity

startActivityForResult(toNextIntent,  REQUEST_ASK);

// 开启 Intent 时候  ,把请求码同时传递

在源请求 Activity 中等待 Intent 返回应答结果,通过重载 onActivityResult()方法

@Override

protected  void  onActivityResult(int requestCode,

  int  resultCode,  Intent  data)  {

//  TODO   Auto-generated  method  stub

super.onActivityResult(requestCode,  resultCode,  data);

if(requestCode==REQUEST_ASK){

if(resultCode==RESULT_CANCELED){

setTitle("Cancel****");

}else if(resultCode==RESULT_OK){

showBundle=data.getExtras();// 从返回的 Intent 中获得 Bundle

Name=showBundle.getString("myName" );// 从 bundle 中获得相应数据

text.setText("the  name  get  from  the  second  layout:\n"+Name);

}

}

}

☻   第一个参数是你开启请求 Intent 时的对应请求码,可以自己定义。

☻   第二个参数是目标 Activity 返回的验证结果码

☻   第三个参数是目标 Activity 返回的 Intent

目标 Activity 中发送请求结果代码,连同源 Activity 请求的数据一同绑定到 Bundle中通过 Intent 传回源请求 Activity 中

backIntent=new  new  new  new  Intent();

stringBundle=new  new  new  new  Bundle();

stringBundle.putString("myName",  Name);

backIntent.putExtras(stringBundle);

setResult(RESULT_OK,  backIntent);// 返回 Activity 结果码

finish();