使用Intent传递数据的有两种方式。
1.通过Intent的putExtra()方法传递数据
putExtra()方法包含两个参数,第一个参数表示传递的数据名称,第二个参数表示传递的数据信息。
例子:
Intent i=new Intent();
i.setClass(MainActivity.this,SecondActivity.class);//设置跳转到的Activity
i.putExtra("test","123");//传递的值
i.putExtra("test2","456");
startActivity(i);
通过putExtra()方法将传递的数据存储在Intent对象后,通关getXXXExtra()方法来获取数据。
Intent i=getIntent();
String name=i.getStringExtra("test");
String name2=i.getStringExtra("test2");
2.使用Bundle类传递数据
Bundle类与Map接口比较类似,都是通过键值对的形式来保存数据。
Intent i=new Intent();
i.setClass(this,SecondActivity.class);
Bundle bundle=new Bundle();//创建Bundle对象
bundle.putString("test","123");
bundle.putString("test2","456");
i.putExtras(bundle);
startActivity(i);
获取数据
Bundle bundle=getIntent().getExtras();
String test=bundle.getString("test");
String test2=bundle.getString(test2"):