借助Intent实现Android工程中Activity之间Java对象的传递有两种方式:一种所传递对象实现了Serializable接口;另一种是所传递对象实现了Parcelable接口,本博客总结传递对象实现Serializable接口的情况下如何实现Java对象传递:
代码1、添加名为“User.java”的文件:
package com.ghj.vo;import java.io.Serializable;public class User implements Serializable{ private static final long serialVersionUID = -8278907591742219230L; private String id; private String name; public User(String id, String name) { this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
代码2、添加名为“FromActivity.java”的文件:
package com.ghj.activity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import com.ghj.vo.User;public class FromActivity extends Activity implements OnClickListener { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.from); button = (Button)findViewById(R.id.ok_btn); button.setOnClickListener(this); } @Override public void onClick(View view) { Intent intent = new Intent(FromActivity.this, ToActivity.class); Bundle bundle = new Bundle(); User user = new User("56862586-108e-4749-93ca-440cd576ba92","王翔"); bundle.putSerializable("user", user); intent.putExtras(bundle); startActivity(intent); }}
代码3、添加名为“ToActivity.java”的文件:
package com.ghj.activity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;import com.ghj.vo.User;public class ToActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.to); TextView textView = (TextView)findViewById(R.id.show_user_info); Intent intent = getIntent(); User user = (User) intent.getSerializableExtra("user"); textView.setText(user.getId() + ":" + user.getName()); }}
代码4、添加名为“from.xml”的文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/ok_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_margin="10dp" android:text="传递Java对象" /></RelativeLayout>
代码5、添加名为“to.xml”的文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/show_user_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true"/></RelativeLayout>
注意:采用传递对象实现Serializable接口的方式实现Java对象的传递,那么所传递对象的类一定要序列化(比如上面User类实现了Serializable),否则会出现类似如下的异常:java.lang.ClassCastException: com.ghj.vo.User cannot be cast to java.io.Serializable
【0分下载Demo】