In Android development, it is sometimes necessary to pass data from one activity to another activity, where simple data types such as String, int, float, etc. are already encapsulated in the bundle. But what if we want to pass a complex data type, such as a book object?
Take a closer look at the method in the bundle, one of which is the Putserializable () method, the Serializable object is a recoverable object interface, we only need to let the book object implement Serializable interface, You can pass the book object using the Bundle.putserializable () method. Don't say much nonsense, now put the code on:
Book Class:
Package Com.bundletest.model.fneg;
Import java.io.serializable;/**
* @Copyright: Copyright (c) 2008–2100
* @Company: Sagret
* @Author: Fengcunhan [email protected]
* @Package: Com.bundletest.model.fneg
* @FileName: Book.java
* @Time: 2010-12-19
* @User: Feng
*/
public class book implements Serializable {
/**
*
*/
Private static final long serialversionuid = 1L;
private String name;
Private String ID;
Private String author;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String getId () {
return ID;
}
public void SetId (String id) {
This.id = ID;
}
Public String Getauthor () {
return author;
}
public void Setauthor (String author) {
This.author = author;
}
}
Instantiate the book class, get the book object book, and set the member variables:
if (Textutils.isempty (bookname) | | Textutils.isempty (author) | | Textutils.isempty (ID)) {
Toast.maketext (androidbundleactivity.this, "input box cannot be empty", Toast.length_short). Show ();
}else{
Book Book=new book ();
Book.setname (BookName);
Book.setauthor (author);
Book.setid (ID);
Intent intent=new Intent (androidbundleactivity.this,recieveactivity.class);
Bundle Bundle=new Bundle ();
Bundle.putserializable ("book", book);
Intent.putextras (bundle);
StartActivity (Intent);
}
Gets the book object passed over in another activity and displays:
Intent intent=this.getintent ();
Bundle Bundle=intent.getextras (); Book book=-bundle.getserializable ("book"); Nametext.settext ("title:" +book.getname ()); Authortext.settext ("" +book.getauthor ()); Idtext.settext ("ID:" +book.getid ());
Android uses bundles to transfer custom types