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均是显示意图。
参考地址:http://www.cnblogs.com/feng88724/archive/2011/02/10/1961225.html
分享到:
相关推荐
android activityIntent 传值 传对象
Android Activity的跳转与传值详解 Activity跳转与传值,主要是通过Intent类来连接多个Activity,以及传递数据。 Intent是Android一个很重要的类。Intent直译是“意图”,什么是意图呢?比如你想从这个Activity...
详细的介绍可以参考我的博客
说道Android中的Activity,如果你做过iOS开发的话,Activity类似于iOS中的ViewController(视图控制器)。在应用中能看到的东西都是放在活动中的。活动是安卓开发比较重要的东西,是用户交互和数据的入口。本篇博客要...
ex07_activity_intent 介绍在android中如何使用intent
只有一个活动的应用也太简单了吧?没错,你的追求应该更高一点。不管你想创建多少 个活动,方法都和上一节中介绍的是...标只会进入到该应用的主活动,那么怎样才能由主活动跳转到其他活动呢?我们现在就来一 起看一看。
[Android开发视频教学].01_05_Activity和Intent,自己手敲得,可以拿去看一下
本源码收集的就是android系统中的一些基本单元,比如弹出的提示框、提示层的应用、漂亮按钮的编写、菜单和列表等,相信对您研究Android系统有帮助。activity的跳转 1.不带返回值的跳转 2.带返回值的跳转
android入门级视频:Android基本组件介绍之Android应用的灵魂——Intent和Activity介绍与实例
自己项目中用到fragment搭建框架,从网上找得一个demo,实现activity与fragment相互传值;fragment与fragment跳转。跟大家分享
Android应用源码之Intent_ComponentSample_Intent
Activity生命周期及Intent传值
android 1.6版本的 INTENT和BUNDLE,即页面跳转和传值
用Intent进行跳转并传值给下一个Activity.
android开发中Intent在两个Activity间传值示例
演示了android系统中页面跳转,页面间传值的方法。Intent的用法
Android_Activity和Intent机制学习笔记
Android Intent多种传值方式 https://blog.csdn.net/qq_31939617/article/details/80742103
1、设计界面,包括四个按钮,两个文本框。单击对应按钮可以启动对应...假定Activity A为启动Activity。 2、修改 Activity 的启动模式 LaunchMode,并通过 Log 信息来体会不同启动模式所对应的Activity 和 task 的关系
1.使用 Intent 和 Bundle 实现页面之间的参数传递和跳转; 2.ListView 的使用以及 ListView 中 Item 的点击事件处理 3.button点击事件