Activity系列讲解---数据传递

时间:2021-02-07 15:38:07

  在Android中,不同的Activity实例可能运行在一个进程中,也可能运行在不同的进程中。因此需要一种特别的机制帮助我们在Activity之间传递消息。Android中通过Intent对象来表示一条消息,一个Intent对象不仅包含有这个消息的目的地,还可以包含消息的内容,好比一封Email,其中不仅应该包含收件地址,还可以包含具体的内容。对于一个Intent对象,消息“目的地”是必须的,而内容是可选的。

1.数据传递的两种方式:

Activity系列讲解---数据传递

Activity系列讲解---数据传递

2.对象的传递:

(1)实现Serializable接口,intent.putExtra(String name,Serializable value)

(2)实现Parcelable接口,intent.putExtra(String name,Parcelable value)

3.代码实现

(1)FirstActivity.class  

/**
* Activity传递数据的两种方式:
* 1.直接通过Bundle对象来传递
* 2.使用Intent定义的Bundle对象
*/
public class FirstActivity extends AppCompatActivity { private EditText ed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first); ed = (EditText) findViewById(R.id.et_data_name);
} //传递基本数据
public void sendDataClick(View view){
String info = ed.getText().toString();
Intent intent = new Intent(this,SecondActivity.class);
//1.直接通过Bundle对象来传递
/* Bundle bundle = new Bundle();
bundle.putString("info",info);
intent.putExtra("data",bundle);*/ //2.使用Intent定义的Bundle对象
intent.putExtra("info",info);
startActivity(intent);
} //传递对象一
public void sendObjectOneClick(View view){
Cat cat = new Cat();
cat.name = "波斯猫";
cat.age = 5;
cat.type = "英短"; Intent intent = new Intent(this,SecondActivity.class);
//putExtra(String name,Serializable value)---Cat.class必须实现Serializable接口
intent.putExtra("cat",cat);
startActivity(intent);
} //传递对象二
public void sendObjectTwoClick(View view){
Dog dog = new Dog();
dog.name = "乐乐";
dog.age = 6;
dog.type = "哈士奇"; Intent intent = new Intent(this,SecondActivity.class);
//putExtra(String name,Parcelable value)---Dog.class必须实现SerializableParcelable接口
intent.putExtra("dog",dog);
startActivity(intent);
} }

  

(2)SecondActivity.class

public class SecondActivity extends AppCompatActivity {

    private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); tv = (TextView) findViewById(R.id.tv_data_display); Intent intent = getIntent();
//1.直接通过Bundle对象来传递
/*
Bundle data = intent.getBundleExtra("data");
String ret = data.getString("info");*/ //2.使用Intent定义的Bundle对象
String ret = intent.getStringExtra("info"); Cat cat = (Cat) intent.getSerializableExtra("cat"); Dog dog = (Dog) intent.getParcelableExtra("dog"); tv.setText(dog.toString());
}
}

  

(3)activity_first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.langdon.taiyang.androidtest.activity.FirstActivity"> <EditText
android:id="@+id/et_data_name"
android:hint="请输入信息"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_data_send"
android:text="发送"
android:onClick="sendDataClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_data_send_object"
android:text="传递对象一"
android:onClick="sendObjectOneClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_data_send_data"
android:text="传递对象二"
android:onClick="sendObjectTwoClick"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

  

(4)activity_second.xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.langdon.taiyang.androidtest.activity.SecondActivity">
<TextView
android:id="@+id/tv_data_display"
android:text="no data"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

  

(5)Cat.class

  

/**
* Cat实现Serializable接口,
* intent.putExtra(String name,Serializable value)---Cat.class必须实现Serializable接口
*/ public class Cat implements Serializable {
public String name;
public int age;
public String type; @Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", age=" + age +
", type='" + type + '\'' +
'}';
}
}

  

(6)Dog.class

    

/**
* Dog实现Parcelable接口,
* intent.putExtra(String name,Parcelable value)---Cat.class必须实现Parcelable接口
*/ public class Dog implements Parcelable{
public String name;
public int age;
public String type; @Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", type='" + type + '\'' +
'}';
} //描述内容
@Override
public int describeContents() {
return 0;
} //将传入的数据写到包裹里面(Parcel)
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
dest.writeString(type);
} //对象的创建器
public static final Parcelable.Creator<Dog> CREATOR
= new Parcelable.Creator<Dog>() {
public Dog createFromParcel(Parcel in) {
Dog dog = new Dog();
dog.name = in.readString();
dog.age = in.readInt();
dog.type = in.readString();
return dog;
} public Dog[] newArray(int size) {
return new Dog[size];
}
}; }