Please explain in detail what Parcelable is on Android.

Asked 2 years ago, Updated 2 years ago, 42 views

I heard that I have to use Parcelable when I make an app and deliver the object. But I don't know what Parcelable is. Please tell me the concept of how to use it.

android parcelable

2022-09-21 19:28

1 Answers

When creating an app, it is often necessary to deliver custom classes or objects to other components as well as basic types such as String, int, and boolean through Intent. In that case, it cannot simply be put into the intent as putExtra(). On Android, there is a class called Parcelable, similar to Java's concept of Serialization for that case.

First, let's look at why this is necessary. For example, suppose you have the following classes:

public class BookData {
  int _id;
  String title;
  String author;
  String publisher;
  int price;
}

If you try to put the information of the books into ArrayList and hand them over to the intent to display them on the screen as ListView in the book management app, you cannot use the BookData class as it is.

To make an object a Parcelable class, you must implement the android.os.Parcelable interface. Therefore, change the class definition as follows.

public class BookData implements Parcelable {
  int _id;
  String title;
  String author;
  String publisher;
  int price;
}

You must override the two methods in the android.os.Parcelable interface.

describeContents() - Defines the type of object to parcel. writeToParcel (Parcel dest, int flags) - a method for serializing/flattening real objects. Each element of the object must be parcelled.

public void writeToParcel(Parcel dest, int flags) {
  dest.writeInt(_id);
  dest.writeString(title);
  dest.writeString(author);
  dest.writeString(publisher);
  dest.writeInt(price);
}

The next thing to do is to add a step of un-marshal/de-serializing data in Parcel. To do so, a variable called CREATOR of the Parcelable.Creator type must be defined. If this variable is not defined, Android will have the following excitations:

Parcelable protocol requires a Parcelable.Creator object called CREATOR

Below is the code of Parcelable.Creator for the BookData class, which is an example above.

public class CustomCreator implements Parcelable.Creator<BookData> {
  public BookData createFromParcel(Parcel src) {
    return new BookData(src);
  }

  public BookData[] newArray(int size) {
    return new BookData[size];
  }
}

BookData.You must define a constructor to recover all parcelled data in java.

public BookData(Parcel src) { _id = src.readInt(); title = src.readString(); author = src.readString(); publisher = src.readString(); price = src.readInt(); }

Note that it should be recovered in the same order recorded in the writeToParcel() method.

The full code is as follows.

...
public class BookData implements Parcelable {
    private String title;
    private String author;
    private String publisher;
    private String isbn;
    private String description;
    private int price;
    private String photoUrl;

    public BookData() {
    }

    public BookData(Parcel in) {
       readFromParcel(in);
    }

    public BookData(String _title, String _author, String _pub, String _isbn, String _desc, int _price, String _photoUrl) {
         this.title = _title;
         this.author = _author;
         this.publisher = _pub;
         this.isbn = _isbn;
         this.description = _desc;
         this.price = _price;
         this.photoUrl = _photoUrl;
    }

// -------------------------------------------------------------------------
// Getters & Setters section - get/set methods for each field
// It's omitted here
// ....
// ....
// -------------------------------------------------------------------------


   public void writeToParcel(Parcel dest, int flags) {
           dest.writeString(title);
           dest.writeString(author);
           dest.writeString(publisher);
           dest.writeString(isbn);
           dest.writeString(description);
           dest.writeString(photoUrl);
           dest.writeInt(price);
   }

   private void readFromParcel(Parcel in){
           title = in.readString();
           author = in.readString();
           publisher = in.readString();
           isbn = in.readString();
           description = in.readString();
           photoUrl = in.readString();
           price = in.readInt();
   }

   public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public BookData createFromParcel(Parcel in) {
             return new BookData(in);
       }

       public BookData[] newArray(int size) {
            return new BookData[size];
       }
   };
}

When sending a Parcelable object to the Intent, it can be done as follows.

BookData book = new BookData(); // Put a value in each field

Intent i = new Intent(this, ShowBook.class);
i.putExtra("bookInfo", book);
startActivity(i);

ShowBook to receive an introduction.In java, you can recover the Parcelable object as follows.

Bundle bundle = getIntent().getExtras();
BookData book = bundle.getParcelable("bookInfo");

In the case of ArrayList, the side that makes and sends Intent can do as follows.

ArrayList<BookData> bookList = new ArrayList<BookData>();
...
// Add a BookData entry to the bookList using the bookList.add() method
...

Intent i = new Intent(this, BookList.class);
i.putParcelableArrayListExtra("myBooks", bookList);
startActivity(i);

In BookList.java (an activity called by the int), you can recover the object as follows.

ArrayList<BookData> bookList;
...
Intent i = getIntent();
bookList = i.getParcelableArrayListExtra("myBooks");

I brought it because there was a good explanation. http://arsviator.blogspot.kr/2010/10/parcelable%EC%9D%84-%EC%82%AC%EC%9A%A9%ED%95%9C-%EC%98%A4%EB%B8%8C%EC%A0%9D%ED%8A%B8-%EC%A0%84%EB%8B%AC-object.html


2022-09-21 19:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.