您现在的位置是:首页 > 文章详情

使用 Bundle在Activity间传递数据

日期:2017-05-03点击:388
使用    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();


本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2010/05/27/1745113.html,如需转载请自行联系原作者


原文链接:https://yq.aliyun.com/articles/343279
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章