Intent界面跳转与传递数据
Activity跳转与传值,主要是通过Intent类,Intent的作用是激活组件和附带数据。
intent可以激活Activity,服务,广播三类组件。
本博文讲的是显示意图激活Activity组件。所谓显示意图就是在activity的激活时,显示指出了需要激活的activity的名字。
一、Activity跳转
方法一
Intent intent = new Intent(A.this, B.class);
startActivity(intent)
方法二
Intent intent = new Intent();
intent.setClass(A.this, B.class);
startActivity(intent);
实现从A跳转到B(A、B均继承自Activity)
二、传递数据
Activity A 传递数据
方法一
Intent intent = new Intent();
intent.setClass(A.this, B.class);
intent.putExtra("name", "xy");
intent.putExtra("age", 22);
startActivity(intent);
方法二
Intent intent = new Intent(A.this, B.class);
Bundle bundle = new Bundle();
bundle.putString("name", "xy");
bundle.putInt("age", 22);
intent.putExtras(bundle);
startActivity(intent);
Activity B 接收数据
// 获取参数1
Intent intent = this.getIntent();
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 22); // 缺省值为22
// 获取参数2
Bundle bundle = intent.getExtras();
String name2 = bundle.getString("name");
int age2 = bundle.getInt("age", 22);
两种获取参数方式均可,并不是和传参1,2方法一一对应
三、Activity返回值
跳转后前一个Activity已经被destroy了。如若要返回并显示数据,就必须将前一个Activity再次唤醒,同时调用某个方法来获取并显示数据。做法如下
1.从A页面跳转到B页面时不可使用startActivity()方法,而要使用startActivityForResult()方法
2.在A页面的Activity中,需要重写onActivityResult(int requestCode, int resultCode, Intent data)方法
Activity A
- // 有返回值的Activity
- public void openNewActivity2(View v)
- {
- Intent intent = new Intent();
- intent.setClass(this.getApplicationContext(), OtherActivity2.class);
- intent.putExtra("name", "xy");
- intent.putExtra("age", 20);
- startActivityForResult(intent, 1);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- // requestCode用于区分业务
- // resultCode用于区分某种业务的执行情况
- if (1 == requestCode && RESULT_OK == resultCode)
- {
- String result = data.getStringExtra("result");
- Toast.makeText(this.getBaseContext(), result, Toast.LENGTH_SHORT).show();
- }
- else
- {
- Toast.makeText(this.getBaseContext(), "无返回值", Toast.LENGTH_SHORT).show();
- }
- }
Activity B
- public void close(View v)
- {
- Intent intent = new Intent();
- intent.putExtra("result", "返回值");
- this.setResult(RESULT_OK, intent); // 设置结果数据
- this.finish(); // 关闭Activity
- }
四、总结
以上采用的意图intent均是显示意图。
Intent界面跳转与传递数据的更多相关文章
- Android Activity间跳转与传递数据
1 概述 Activity之间的跳转主要使用 startActivity(Intent intent); startActivityForResult(Intent intent,int reques ...
- 界面跳转+信息传递+AS中如何将ADV转移到其他盘中
今日所学:界面跳转 信息传递 遇到的问题: 昨天遇到不能新建java类,在网上百度了很多,大多原因是没有新建java类的模板,但是我有,换了一个新的新建的方式后,发现虽然能建立了,但在测试时还是不能页 ...
- 【Android】7.0 Intent向下一个活动传递数据、返回数据给上一个活动
1.0 可以利用Intent吧数据传递给上一个活动,新建一个叫“hellotest01”的项目. 新建活动FirstActivity,勾选“Generate Layout File”和“Launche ...
- 微信小程序:页面跳转时传递数据到另一个页面
一.功能描述 页面跳转时,同时把当前页面的数据传递给跳转的目标页面,并在跳转后的目标页面进行展示 二.功能实现 1. 代码实现 test1页面 // pages/test1/test1.js Page ...
- Android基础Activity篇——Intent向下一个活动传递数据
1.向下一个活动传递数据 String data ="bilibilbilbilbili"; Intent intent1=new Intent(this,secondActivi ...
- 向通知栏发送通知点击跳转并传递数据(PendingIntent)传递数据
// 为发送通知的按钮的点击事件定义事件处理方法 public void send() {///// 第一步:获取NotificationManager NotificationManager nm ...
- Vue Router路由守卫妙用:异步获取数据成功后再进行路由跳转并传递数据,失败则不进行跳转
问题引入 试想这样一个业务场景: 在用户输入数据,点击提交按钮后,这时发起了ajax请求,如果请求成功, 则跳转到详情页面并展示详情数据,失败则不跳转到详情页面,只是在当前页面给出错误消息. 难点所在 ...
- iOS --- 通过openURL实现APP之间跳转并传递数据
在IOS中,实现一个应用启动另外一个应用,使用UIApplication的openURL:方法就可实现,这里以test跳到test02为例.(需要先创建这两个工程) 注册自定义URL协议(在test中 ...
- Intent 隐式跳转,向下一个活动传递数据,向上一个活动返回数据。
一.每个Intent只能指定一个action,多个Category. 使用隐式跳转,我们不仅可以跳转到自己程序内的活动,还可以启动其他程序的活动.使得Android多个程序之间的功能共享成为可能. 例 ...
随机推荐
- Java for LeetCode 217 Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- python中的编码问题:以ascii和unicode为主线
1.unicode.gbk.gb2312.utf-8的关系 http://www.pythonclub.org/python-basic/encode-detail 这篇文章写的比较好,utf-8 ...
- c语言实现面向对象OOC
这种问题比较锻炼思维,同时考察c和c++的掌握程度.如果你遇到过类似问题,此题意义自不必说.如果用c实现c++,主要解决如何实现封装,继承和多态三大问题,本文分两块说. 1.封装 // Example ...
- android学习————项目导入常见错误整理(转载)
详细请访问http://4789781.blog.51cto.com/4779781/1031107
- C# 对象深度拷贝
转载 using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using ...
- xcode7 免费真机调试
原文链接:http://www.cnblogs.com/tandaxia/p/4839997.html 刚新安装了Xcode7 Version 7.1 beta , 据说这个版本可以免费真机调试,于是 ...
- 超链接弹出QQ对话框
<a href="tencent://message/?uin=371820621">java技术交流群57388149</a>
- struts.xml配置
1. package标签 package:完成有业务相关的Action(应用控制器的)管理 name:给包起的名字(反映该包中Action的功能),用来完成包和包之间的继承.默认继承struts-de ...
- 【2016-10-11】Linux系统常用的关机或重启命令shutdown、reboot、halt、poweroff、init 0及init 6的联系与区别
Linux下常用的关机/重启命令一般包括: shutdown.reboot.halt.poweroff等,当然了我们可以使用init 运行等级runlevel 0即halt来关机,或使用init 运行 ...
- PullToRefreshScrollView嵌套SwipeMenuListView冲突问题解决
参考: http://blog.csdn.net/u012255016/article/details/46048797 public class NoScrollSwipeMenuListView ...