Intent 的几种跳转方式
第一种 直接界面A跳转到界面B
Intent intent = new Intent(A类名.this, B类名.class);
startActivity(intent);
第二种 界面A跳转到界面B传一个字符串
A界面代码
Intent intent = new Intent(A类名.this, B类名.class);
intent.putExtra("字符串的名字", "字符串的内容");
startActivity(intent);
B界面代码
Intent intent=getIntent();
String str=intent.getStringExtra("A中定义字符串的名字");
A中字符串的内容保存到str中
第三种 利用bundle传字符串
Intent intent = new Intent(A类名.this, B类名.class);
Bundle bundle=new Bundle();
bundle.putString(“key”, “value”);
intent.putExtra(“bundle”, bundle);
startActivity(intent);
接收
Intent intent=getIntent();
Bundle bundle=intent.ge tBundleExtra("bundle");
System.out.println(bundle.getString("key"));
第四种 传输对象User 定义的时候最好是实现序列化
A界面代码
Intent intent = new Intent(A类名.this, B类名.class);
Bundle bundle=new Bundle();
bundle.putString("info", "value");
User use=new User("xiaohai",20);
bundle.putSerializable("user", use);
intent.putExtra("bundle", bundle);
startActivity(intent);
B界面代码
Intent intent=getIntent();
Bundle bundle=intent.getBundleExtra("bundle");
User use=(User)bundle.getSerializable("user");
System.out.println(bundle.getString("info")+""+use.toString());
最后记得要在清单文件 AndroidManifest.xml 中添加
<activity
android:name="com.example.包名.B类"
android:label="@string/app_name" >
</activity>