tutorial, no_image, android,

Android - no_image

Upendra Upendra Follow Jan 23, 2025 · 1 min read
Android - no_image
Share this

Parcelable

Parcelable used for pass data between components. Parcelable is the analog of Java Serializable interface with optimization for mobile devices. It assumes a certain structure and way of processing it.

Classes implementing the Parcelable interface must also have a non-null static field called CREATOR of a type that implements the Parcelable.Creator interface.

A typical implementation of Parcelable is:

 public class MyParcelable implements Parcelable {
     private int mData;

     public int describeContents() {
         return 0;
     }

     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(mData);
     }

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

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

     private MyParcelable(Parcel in) {
         mData = in.readInt();
     }
 }

We can now pass the parcelable data between activities within an intent:

MyParcelable info = new MyParcelable();
Intent i = new Intent(this, NewActivity.class);
i.putExtra("parcelableKey", info);
startActivity(i);

For obtaining information in NewActivity should do next:

public class NewActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        MyParcelable info = (MyParcelable) getIntent().getParcelableExtra("parcelableKey");
    }
}

Resources for simplified generate the boilerplate code for creating Parcelables:

https://developer.android.com/reference/android/os/Parcelable
https://guides.codepath.com/android/using-parcelable
https://www.vogella.com/tutorials/AndroidParcelable/article.html

credit goes to @swayangjit
Join Newsletter
Get the latest news right in your inbox. We never spam!
Upendra
Written by Upendra Follow
Hi, I am Upendra, the author in Human and machine languages,I don't know to how 3 liner bio works so just Connect with me on social sites you will get to know me better.