</pre><pre name="code" class="java"><p>Intent intent = new Intent(ThreeActivity.this, FourActivity.class); intent.putExtra("name", "Echo"); intent.putExtra("age", 24); startActivity(intent);</p>
</pre><pre>
Intent intent = getIntent(); if(intent != null){ String name = intent.getStringExtra("name"); int age = intent.getIntExtra("age",0); textView.setText("name="+ name + " age=" + age); }
通过Bundle传递数据(Bundle是负责底层跨进程间传递的一个通信协议)
在ThreeActivity.class中添加如下demo
Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putString("name", "Echo"); bundle.putInt("age", 24); intent.putExtras(bundle); startActivity(intent);
传递一个对象
package com.echo.activity; import java.io.Serializable; public class Person implements Serializable { private int age; private String name; private String address; public Person(int age, String name, String address) { super(); this.age = age; this.name = name; this.address = address; } @Override public String toString() { // TODO Auto-generated method stub return "[name=" + name + "age=" + age + "address=" + address + "]"; } }
Intent intent = new Intent(ThreeActivity.this, FourActivity.class); Person person = new Person(24, "Echo", "MianYang"); Bundle bundle = new Bundle(); bundle.putSerializable("person", person); intent.putExtras(bundle); startActivity(intent);
Intent intent = getIntent(); if(intent != null){ Person person = (Person) intent.getSerializableExtra("person"); textView.setText(person.toString()); }
传递一个Bitmap
Intent intent = new Intent(ThreeActivity.this, FourActivity.class); Bundle bundle = new Bundle(); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); bundle.putParcelable("bitmap", bitmap); intent.putExtras(bundle); startActivity(intent);
Intent intent = getIntent(); if(intent != null){ Bitmap bitmap = intent.getParcelableExtra("bitmap"); imageView.setImageBitmap(bitmap); }
传递一个大数据
Intent intent = new Intent(ThreeActivity.this, FourActivity.class); Bundle bundle = new Bundle(); int[] data = new int[1024*1024*8]; bundle.putIntArray("data", data); intent.putExtras(bundle); startActivity(intent);
Intent intent = getIntent(); if(intent != null){ int[] data = intent.getIntArrayExtra("data"); Log.i("data", "data="+data); }
出现异常
创建一个大的Bitmap
Intent intent = new Intent(ThreeActivity.this, FourActivity.class); Bundle bundle = new Bundle(); //480*1200 占有4字节的bitmap Bitmap bitmap = Bitmap.createBitmap(480, 1200, Config.ARGB_8888); bundle.putParcelable("bitmap", bitmap); intent.putExtras(bundle); startActivity(intent);
<pre name="code" class="java"> Intent intent = getIntent(); if(intent != null){ Bitmap bitmap = intent.getParcelableExtra("bitmap"); imageView.setImageBitmap(bitmap); }
出现异常