Activity学习基础 Activity使用Serializable传递对象实例
在Intent中我们使用putExtras方法携带数据,在例子中我们就介绍将数据存放到Bundle中,然后putExtras带上Bundle参数,代码如下:
public class ActivityA extends Activity {
private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_for_a);
// 设置当前的的Activity的标题,以作为标记使用
setTitle("这是Activity A");
// 获取布局文件中的button
button = (Button) findViewById(R.id.a_button);
// 给button设置文字
button.setText("A跳B");
// button添加监听事件
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 调用AtoB
AtoB();
}
});
}
/**
* 这个方法实现从当前Activity(A)跳转到Activity(B)
* */
public void AtoB() {
// intent很重要,是两个Activity之间的纽带
Intent in = new Intent();
// 使用intent连接两个Activity,一个参数是当前的上下文(context),还有一个是要切换到的Activity主类
in.setClass(this, ActivityB.class);
//需要传出去的数据字串
String hello = "我是来自Bundle中的数据";
//我们把要传出去的字串放到bundle中
Bundle extras = new Bundle();
//第一个参数是key值,取的通过这个key就可以拿到这个bundle中的数据了
extras.putString("intent_bundle", hello);
//将bundle放进Intent中
in.putExtras(extras);
// 跳转
startActivity(in);
}
}
但是现在的需求是如何在Activity中传递对象实例呢?就我目前所知的有两种,分别是java中Serializable和Android新引进的Parcelable方法。
使用Serializable方法,我们必须在定义对象类的时候实现Serializable接口,我这边写一个书籍类,代码如下:
public class SerializableBook implements Serializable {
private static final long serialVersionUID = 4226755799531293257L;
private String Name;
private String Author;
private String Pubdate;
private float Price;
/**
* @param name the name to set
*/
public void setName(String name) {
Name = name;
}
/**
* @return the name
*/
public String getName() {
return Name;
}
/**
* @param author the author to set
*/
public void setAuthor(String author) {
Author = author;
}
/**
* @return the author
*/
public String getAuthor() {
return Author;
}
/**
* @param pubdate the pubdate to set
*/
public void setPubdate(String pubdate) {
Pubdate = pubdate;
}
/**
* @return the pubdate
*/
public String getPubdate() {
return Pubdate;
}
/**
* @param price the price to set
*/
public void setPrice(float price) {
Price = price;
}
/**
* @return the price
*/
public float getPrice() {
return Price;
}
}
然后起一个Activity A,代码如下:
public class ActivityA extends Activity {
private String SerializableKey = "ourunix_serialzable";
private Button mButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_for_a);
initView();
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tranSerializableObject();
}
});
}
public void initView(){
mButton = (Button) findViewById(R.id.a_button);
mButton.setText("A跳B");
}
public void tranSerializableObject(){
Intent in = new Intent();
in.setClass(ActivityA.this, ActivityB.class);
//实例化一个SerializableBook对象
SerializableBook book = new SerializableBook();
book.setAuthor("walfred");
book.setName("How to learn Android");
book.setPrice(10.00f);
book.setPubdate("2014-01-01");
Bundle extras = new Bundle();
extras.putSerializable(SerializableKey, book);
in.putExtras(extras);
startActivity(in);
}
}
最后在Activity B中接受这个对象,并展示出来,代码如下:
public class ActivityB extends Activity {
private String SerializableKey = "ourunix_serialzable";
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_for_b);
initView();
getAndShowSerialzableObeject();
}
public void initView(){
mTextView = (TextView)findViewById(R.id.b_textview);
}
public void getAndShowSerialzableObeject(){
Bundle extras = getIntent().getExtras();
if (extras != null){
SerializableBook book = (SerializableBook) extras.get(SerializableKey);
mTextView.setText("Name:" + book.getName()+"\\n"
+ "Author:" + book.getAuthor() + "\\n"
+ "Pubdate:" + book.getPubdate() + "\\n"
+ "Price:" + book.getPrice());
}else{
mTextView.setText("nothing");
}
}
}