笔记 Activity之间跳转与传值方式

来源:互联网 发布:软件搜索神器 编辑:程序博客网 时间:2024/06/04 00:43

==============
Activity之间跳转与传值方式
--------------------
跳转方式(两种)
------
第一种:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
----
第二种:
Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
startActivity(intent);

传值方式
-------
只需要在跳转中加入一句:
intent.putExtra("Name", "ppy2790");    就行了
例如:
Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
intent.putExtra("Name", "ppy2790");
startActivity(intent);
-------------------
这是对于传递数据量较少的,采用上述方法;
对于传递数据较多的采用以下方式:
使用Bundle类
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
 
/* 通过Bundle对象存储需要传递的数据 */
Bundle bundle = new Bundle();
/*字符、字符串、布尔、字节数组、浮点数等等,都可以传*/
bundle.putString("Name", "ppy2790");
bundle.putBoolean("Ismale", true);
 
/*把bundle对象assign给Intent*/
intent.putExtras(bundle);
 
startActivity(intent);
-----------------

接收
-------
对于用了Bundle类的接收:
 /*获取Intent中的Bundle对象*/
Bundle bundle = this.getIntent().getExtras();
         
 /*获取Bundle中的数据,注意类型和key*/
String name = bundle.getString("Name");
boolean ismale = bundle.getBoolean("Ismale");














0 0
原创粉丝点击