Intent实现页面跳转和传值
*Intent称为意图,是Android各大组件连接的桥梁
1.Activity页面跳转
同一个包内
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
MainActivity.this.startActivity(intent);
不同包内
(1)明确的Intent
Intent intent = new Intent();
ComponentName comp = new ComponentName("包名(应用本身所在的包,通过AndroidManifest.xml中package属性查看)","包名.类名(Activity所在的包)");
//或者intent.setClassName("包名","包名.类名");
intent.setComponent(comp);
startActivity(intent);
(2)不明确的Intent
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN); // 应用程序的入口
intent.addCategory(Intent.CATEGORY_HOME); // 桌面的应用程序
startActivity(intent);
然后在AndroidManifest.xml中注册该过滤条件
<activity
android:name=".OtherActivity"
android:label="OtherActivity" >
<intent-filter>
<action android:name="action_name" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
注:
*第一个Activity是外部通过不明确的Intent跳转的
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
2.Activity页面跳转传值
第一种方法:(Intent)
发送方:
Intent intent = new Intent();
intent.putExtra("name", "诸葛亮");
intent.putExtra("age", 50);
intent.putExtra("IQ", 200.0f);
intent.setClass(MainActivity.this, SecondActivity.class);
MainActivity.this.startActivity(intent);
接受方:
Intent intent = getIntent();
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 0);
float IQ = intent.getFloatExtra("IQ", 0.0f);
textview2.setText("name:"+name+",age:"+age+",IQ:"+IQ);
第二种方法:(Bundle)
发送方:
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("name", "乔峰");
bundle.putInt("age", 40);
bundle.putFloat("weight", 70.4f);
intent.putExtras(bundle);
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
接受方:
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
int age = bundle.getInt("age");
float weight = bundle.getFloat("weight");
textview.setText(name+","+age+","+weight);
第三种方法:(Application)
首先要新建一个继承Application的MyApp类,补充属性的get/set方法
AndroidManifest.xml也要配置application的属性android:name=".MyApp"
发送方:
MyApp myApp = (MyApp) getApplication();
myApp.setName("周星驰");
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
接受方:
MyApp myApp = (MyApp) getApplication();
String name = myApp.getName();
textview = (TextView) findViewById(R.id.textview);
textview.setText(name);
第四种方法:(推荐)
对象类要实现Parclelable接口
发送方:
Intent intent = new Intent();
Person person = new Person("孙悟空",500,55.6f);
intent.putExtra("person", person);
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
接受方:
Intent intent = getIntent();
Person person = intent.getParcelableExtra("person");
textview = (TextView) findViewById(R.id.textview);
textview.setText(person.toString());
对象类:
public class Person implements Parcelable {
private String name;
private int age;
private float weight;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age, float weight) {
super();
this.name = name;
this.age = age;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", weight=" + weight + "]";
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
dest.writeFloat(weight);
}
public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
public Person createFromParcel(Parcel in) {
return new Person(in);
}
public Person[] newArray(int size) {
return new Person[size];
}
};
private Person(Parcel in) {
name = in.readString();
age = in.readInt();
weight = in.readFloat();
}
}
3.页面返回传值
被返回方:
startActivityForResult(intent, 38);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bundle bundle = data.getExtras();
String name = bundle.getString("name");
int age = bundle.getInt("age");
float weight = bundle.getFloat("weight");
Toast.makeText(MainActivity.this, name+age+weight, Toast.LENGTH_LONG).show();
super.onActivityResult(requestCode, resultCode, data);
}
返回方:
Intent data = new Intent();
data.setClass(SecondActivity.this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name", "张无忌");
bundle.putInt("age", 20);
bundle.putFloat("weight", 120.5f);
data.putExtras(bundle);
setResult(250, data);
finish();
启动Activity
上下文中,通过如下方法启动Activity
*startActivity(Intent intent):启动新的Activity
*startActivityForResult(Intent intent, int requestCode):指定请求码启动新的Activity
如果使用startActivityForResult()启动Activity,则必须重写onActivityResult()方法,并且该方法第一个参数与requestCode相对应
关闭Activity
上下文中,通过如下方法关闭Activity
*finish():关闭当前Activity
*finishActivity(int requestCode):关闭以startActivity启动的Activity
在调用finish()关闭当前Activity前,可以调用setResult(int resultCode)设置返回码。返回码在启动的Activity中通过onActivityResult()捕获
欢迎关注我的微信公众号:安卓圈

Intent实现页面跳转和传值的更多相关文章
- Android中实现activity的页面跳转并传值
一个Android应用程序很少会只有一个Activity对象,如何在多个Activity之间进行跳转,而且能够互相传值是一个很基本的要求. 本次我们就讲一下,Android中页面跳转以及传值的几种方式 ...
- iOS——使用StroryBoard页面跳转及传值
之前在网上搜iOS的页面跳转大多都是按回以前的那种xib的形式,但鄙人是使用storyboard的.这篇就只介绍利用storyboard进行页面跳转与传值. 新建页面 iOS的程序也是使用了MVC的思 ...
- Intent实现页面跳转
Intent实现页面跳转: 1. startActivity(intent) 2. startActivityForResult(intent,requestCode); onActivityResu ...
- iOS使用StroryBoard页面跳转及传值
之前在网上iOS的页面跳转大多都是按回以前的那种xib的形式,但鄙人是使用storyboard的.这篇就只介绍利用storyboard进行页面跳转与传值. 新建页面 iOS的程序也是使用了MVC的思想 ...
- H5页面跳转与传值
页面之间的跳转经常使用a标签,使用mvc框架的都是通过访问controller的请求方法,返回请求页面.但本次开发,前端与后台完全分离,前端APP使用HBuider来开发,后台数据就无法使用mvc框架 ...
- flutter -------- 页面跳转和传值
在安卓原生开发中,页面跳转可以用Intent类来具体实现: Intent intent =new Intent(MainActivity.this,second.class); startActivi ...
- Android成长日记-使用Intent实现页面跳转
Intent:可以理解为信使(意图),由Intent来协助完成Android各个组件之间的通讯 Intent实现页面之间的跳转 1->startActivity(intent) 2->st ...
- MVC 【Razor 视图引擎】基础操作 --页面跳转,传值,表单提交
ASPX 与 Razor 仅仅是视图不一样. 新建项目----ASP.NET MVC 4 Web 应用程序------选择模板(空).视图引擎(Razor ) 1.视图中 c# 代码 与 HT ...
- swift 下storyboard的页面跳转和传值
------------------1. 最简单的方法 拖拽, 这个就不用多解释了吧. 直接拖拽到另一个视图控制器, 选择 show, 就行了. 2. 利用 Segue 方法 (这里主要是 方法1 的 ...
随机推荐
- 反射中 GetCustomAttributes
public abstract object[] GetCustomAttributes(bool inherit); 这是GetCustomAttributes方法的一个重载,参数为bool类型返回 ...
- window.document
<title>无标题文档</title> </head> <body>例1:<br />今年是哪一年?<input type=&quo ...
- CocoStudio基础教程(5)使用CocoStudio场景编辑器关联组件
1.概述 我们有了UI交互.有了动画人物.有了物理模拟,还差最后一步——将这些元素融合起来.这就要用到cocoStudio中的场景编辑器了.这次我们要将先前我们做过的所有东西都放到一个场景中去.这项工 ...
- HDU 4857 Couple doubi(找循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4861 解题报告:桌子上有k个球 ,第i个球的价值wi = 1^i+2^i+...+(p-1)^i (m ...
- [Effective JavaScript 笔记]第53条:保持一致的约定
对于api使用者来说,你所使用的命名和函数签名是最能产生普遍影响的决策.这些约定很重要具有巨大的影响力.它建立了基本的词汇和使用它们的应用程序的惯用法.库的使用者必须学会阅读和使用这些.一致的约定可以 ...
- 第12章 使用Samba或NFS实现文件共享
章节简述: 本章节为读者讲述文件共享系统的作用,了解Samba与NFS服务程序的开发背景以及用法. 详细逐条讲解Samba服务配置参数,演示安全共享文件的配置策方法,并使用autofs服务程序自动挂载 ...
- 教程Xcode 4下编译发布与提交App到AppStore
地址:http://www.cocoachina.com/bbs/simple/?t55825.html 教程Xcode 4下编译发布与提交App到AppStore 先说一下这个是我在网上看到的一个帖 ...
- 内存不能为read修复方法:(转自:网上(忘记了))
指令修复法!开始菜单,运行 ,输入cmd, 回车,在命令提示符下输入(复制即可) : for %1 in (%windir%\system32\*.ocx) do regsvr32.exe /s %1 ...
- MongoDB 学习笔记(python操作)
转自: http://blog.csdn.net/daillo/article/details/7030910
- Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...