方式一:使用putExtra,在MainActivity中传输数据,在SecondActivity中输出数据:
package com.dystu.intentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view){ String[] books = new String[]{"编程之美","剑指offer","数学之美"}; Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("name", "hanmeimei"); intent.putExtra("isGirl", true); intent.putExtra("age", 28); intent.putExtra("books", books); startActivity(intent); } }
package com.dystu.intentdemo; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class SecondActivity extends Activity { private String book; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); TextView tv = (TextView) findViewById(R.id.tv); String name = getIntent().getStringExtra("name"); boolean isGirl = getIntent().getBooleanExtra("isGirl", false); int age = getIntent().getIntExtra("age", 0); String[] books = getIntent().getStringArrayExtra("books"); for (String string : books) { System.out.println(string); } tv.setText("name:" + name +"\n" + "isGirl:" + isGirl + "\n" + "age:" + age ); } }
可以看到putExtra所支持的数据类型是非常有限的,当传递一些自定义的对象时,putExtra就做不到了。
方式2:通过Serializable接口
Serializable是序列化的意思,表示将一个对象转换为可存储或者可传输的状态,序列化后的对象可以在网络上进行传输,也可以存储到本地。
比如:
package com.dystu.intentdemo; import java.io.Serializable; public class Person implements Serializable{ private String name; private int age; 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; } }
package com.dystu.intentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view){ Person person = new Person(); person.setName("hanmeimei"); person.setAge(28); Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("person_data", person); startActivity(intent); } }
package com.dystu.intentdemo; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class SecondActivity extends Activity { private String book; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); TextView tv = (TextView) findViewById(R.id.tv); Person person = (Person) getIntent().getSerializableExtra("person_data"); tv.setText("name:" + person.getName() + "\n" + "age:" + person.getAge()); } }
通过getSerializableExtra既可以取出数据。
方式3:通过Parcelable接口
不同将对象进行序列化,Parcelable方式的实现原理是将一个完整的对象进行分解,而分解后的每一部分是Intent所支持的类型。
如下:
package com.dystu.intentdemo; import android.os.Parcel; import android.os.Parcelable; public class Person implements Parcelable{ private String name; private int age; 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; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeInt(age); } public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() { @Override public Person[] newArray(int size) { return new Person[size]; } @Override public Person createFromParcel(Parcel source) { Person person = new Person(); person.name = source.readString(); person.age = source.readInt(); return person; } }; }
MainActivity的代码不变。
package com.dystu.intentdemo; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class SecondActivity extends Activity { private String book; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); TextView tv = (TextView) findViewById(R.id.tv); Person person = (Person) getIntent().getParcelableExtra("person_data"); tv.setText("name:" + person.getName() + "\n" + "age:" + person.getAge()); } }
Parcelable效率高一点。
时间: 2024-10-02 17:38:30