Android页面之间进行数据回传
2016-05-03 16:55
281 查看
要求:页面1跳转到页面2,页面2再返回页面1同时返回数据
页面1添加如下代码:
页面2接收数据添加代码如下:
页面1接收返回数据:(需要重写onActivityResult)
页面1添加如下代码:
Intent intent = new Intent(); intent.setClass(页面1.this, 页面2.class); Bundle bundle = new Bundle(); intent.putExtras(bundle);//将Bundle添加到Intent,也可以在Bundle中添加相应数据传递给下个页面,例如:bundle.putString("abc", "bbb"); startActivityForResult(intent, 0);// 跳转并要求返回值,0代表请求值(可以随便写)
页面2接收数据添加代码如下:
Intent intent = this.getIntent(); Bundle bundle = intent.getExtras(); bundle.putString("aaa", "back");//添加要返回给页面1的数据 intent.putExtras(bundle); this.setResult(Activity.RESULT_OK, intent);//返回页面1 this.finish();
页面1接收返回数据:(需要重写onActivityResult)
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0 && resultCode == Activity.RESULT_OK) { Bundle bundle = data.getExtras(); gameView.backString = bundle.getString("aaa"); Toast.makeText(this, backString, Toast.LENGTH_SHORT).show(); } }
相关文章推荐
- 关于android里的文件创建及读写问题
- Android创建文件夹及文件并写入数据
- android图表——MPAndroidchart
- android学习,在按钮或者文字上添加图片
- Android计算缓存大小、清空缓存
- Android 实现答题器功能(通过手势实现翻页效果)
- Android GridView Item居中(可能是最完美的解决方案)
- Android 之 OO Principle(面向对象的原则)
- Android Loader详解
- android 判断程序是首次(第一次)进入
- Android中的进程与线程
- Android实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
- Android 透明状态栏了解使用 及SystemBarTint 解读
- Android开发:相册读取、拍照、图片裁剪和图片上传服务器等功能的实现
- Android之自定义View的实现
- android开发步步为营之101:动态调整view的宽度和高度以及在页面的坐标位置
- Android Studio 生成APK实践
- Android Studio如何用release签名进行debug调试
- android Button防止多次点击
- Android线程中设置控件