使用 Bundle在Activity间传递数据



使用 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();


优质内容筛选与推荐>>
1、webshell 匿名用户(入侵者)
2、性能:前端的性能到底对业务数据有多大的影响?
3、黑马程序员---ios学习日志 10
4、java.util.vector中的vector的详细用法
5、微信开发之如何使用开发工具--weixin-java-tools


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号