使用Bundle在Activity间传递数据
使用Bundle在Activity间传递数据
public class SourceActivty extends Activity {
private Intent intent=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intent=new Intent();
Bundle myBundelForName=new Bundle();
myBundelForName.putString("Key_Name","1111");
myBundelForName.putString("Key_Age","22");
intent.putExtras(myBundelForName);
TextView text=(TextView)findViewById(R.id.test);
text.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
intent.setClass(SourceActivty.this, TargetActivty.class);
startActivity(intent);
}
});
}
}
目标activty
public class TargetActivty extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intest);
TextView text=(TextView)findViewById(R.id.tt);
//从Intent 中获取数据
Bundle myBundelForGetName=this.getIntent().getExtras();
String name=myBundelForGetName.getString("Key_Name");
text.setText("欢迎您进入:"+name);
}
}
从源请求Activity 中通过一个Intent 把一个服务请求传到目标Activity
源activty
private Intent openWelcomeActivityIntent = null;
TextView text=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
openWelcomeActivityIntent = new Intent();
// 设定开启的下一个Activity
text=(TextView)findViewById(R.id.test);
text.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
openWelcomeActivityIntent.setClass(SourceActivty1.this,
TargetActivty1.class);
startActivityForResult(openWelcomeActivityIntent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_CANCELED) {
setTitle("Cancel****");
} else if (resultCode == RESULT_OK) {
Bundle showBundle = data.getExtras();// 从返回的Intent中获得Bundle
text.setText("the name get from the second layout:\n" + showBundle.getString("myName"));
}
}
}
}
☻ 第一个参数是你开启请求Intent时的对应请求码,可以自己定义。
☻ 第二个参数是目标Activity返回的验证结果码
☻ 第三个参数是目标Activity返回的Intent
目标activty
public class TargetActivty1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intest);
Intent backIntent=new Intent();
Bundle stringBundle=new Bundle();
stringBundle.putString("myName", "lishiyao");
backIntent.putExtras(stringBundle);
setResult(RESULT_OK, backIntent);//返回Activity结果码
finish();
}
}
使用Bundle在Activity间传递数据的更多相关文章
- Android学习手记(3) Activity间传递数据
1. 简单数据传递 建立两个Activity,名称分别为MainActivity和TheAty,在MainActivity中新建一个Button,id为btnStartAty.在TheAty中新建一个 ...
- Activity间传递数据
1.从当前的Activity传递数据到下一个Activity: (1)发送方(当前的Activity): Bundle bundle = new Bundle(); bundle.putString ...
- Android广播接收器和Activity间传递数据
Activity向广播接收器传递数据很简单,只需要在发送广播前将数据put进Intent中就行了. 广播接收器怎么向Activity传送数据?这里要用到接口,通过在广播接收器里定义一个接口,然后让接收 ...
- 利用Bundle在activity之间传递对象
(2010-12-04 09:45:54) 转载▼ 标签: it 分类: android开发 转自:http://chen592969029.javaeye.com/blog/772656 假如需要在 ...
- Activity之间传递数据或数据包Bundle,传递对象,对象序列化,对象实现Parcelable接口
package com.gaojinhua.android.activitymsg; import android.content.Intent; import android.os.Bundle; ...
- 第一课android开发之在activity间传递参数
一.活动间简单参数传递:1.在布局中添加按钮,用<Button,用id设置id名称,id="@+id/这儿填写你要设置成的名称":用text设置按钮上显示的文字.text=& ...
- Activity之间传递数据的方式及常见问题总结
Activity之间传递数据一般通过以下几种方式实现: 1. 通过intent传递数据 2. 通过Application 3. 使用单例 4. 静态成员变量.(可以考虑 WeakReferences) ...
- 28、activity之间传递数据&批量传递数据
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android ...
- 【Android 复习】 : Activity之间传递数据的几种方式
在Android开发中,我们通常需要在不同的Activity之间传递数据,下面我们就来总结一下在Activity之间数据传递的几种方式. 1. 使用Intent来传递数据 Intent表示意图,很多时 ...
随机推荐
- 机器学习框架Tensorflow数字识别MNIST
SoftMax回归 http://ufldl.stanford.edu/wiki/index.php/Softmax%E5%9B%9E%E5%BD%92 我们的训练集由 个已标记的样本构成: ,其 ...
- Homework-09 二维数组动态显示
思路 主要是把计算的函数由一次跑完全部改成一次一步 主要是把一个3重for循环改为能一步一次的实现 public int nstep(int n){ while((n--)!=0){ //n步长 // ...
- Android记事本09
昨天: Activity的数据传递. 今天: 从Activity中返回数据 请求码和返回码的作用 遇到的问题: 无.
- 201621123033 《Java程序设计》第9周学习总结
第九次作业 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 //stream(),filter(),collect() ...
- SQL小助手——SQL Prompt
背景: 当数据库设计的比较复杂.庞大时,我们如果对脚本不是很熟悉,就会很难完成看似简单的增.删.改.查的操作.我们需要一款软件来给出相应的提示或帮助,来提高代码的可读性,更快更好的完成任务. 简介: ...
- hexo 配置文件 实例
# Hexo Configuration ## Docs: https://hexo.io/docs/configuration.html ## Source: https://github.com/ ...
- 详解Linux运维工程师应具备的十大技能
Linux系统如果是学习可以选用Redhat或CentOS,特别是CentOS在企业中用得最多,当然还会有其它版本的,但学习者还是以这2个版本学习就行,因为这两个版本都是兄弟,没区别的,有空可以再研究 ...
- 洛谷 最小费用最大流 模板 P3381
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...
- [LeetCode169]Majority Element
Majority Element Total Accepted: 58500 Total Submissions: 163658My Submissions Question Solution Gi ...
- 导入android源码中的APP源码到eclipse
导入android源码中的APP源码到eclipse 一般最简单的办法就是创建新的android工程,选择create project from existing source选项,直接导入源码就OK ...