Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data through intent using Serializable

I've implemented my class with serializable, but it still didn't work.

This is my class:

package com.ursabyte.thumbnail;  import java.io.Serializable;  import android.graphics.Bitmap;  public class Thumbnail implements Serializable {      private static final long serialVersionUID = 1L;     private String label = "";     private Bitmap bitmap;      public Thumbnail(String label, Bitmap bitmap) {         this.label = label;         this.bitmap = bitmap;     }      public void set_label(String label) {         this.label = label;     }      public String get_label() {         return this.label;     }      public void set_bitmap(Bitmap bitmap) {         this.bitmap = bitmap;     }      public Bitmap get_bitmap(){         return this.bitmap;     }      //  @Override     //  public int compareTo(Thumbnail other) {     //      if(this.label != null)     //          return this.label.compareTo(other.get_label());     //      else     //          throw new IllegalArgumentException();     //  }  } 

This is what I want to be passing.

List<Thumbnail> all_thumbs = new ArrayList<Thumbnail>(); all_thumbs.add(new Thumbnail(string, bitmap)); Intent intent = new Intent(getApplicationContext(), SomeClass.class); intent.putExtra("value", all_thumbs); 

But still it didn't work. I don't know how to use Parcelable, so I use this instead.

like image 345
Bias Tegaralaga Avatar asked Jan 15 '13 08:01

Bias Tegaralaga


People also ask

Why Parcelable is faster than Serializable?

Parcel able is faster than serializable. Parcel able is going to convert object to byte stream and pass the data between two activities. Writing parcel able code is little bit complex compare to serialization. It doesn't create more temp objects while passing the data between two activities.

Can we pass object through intent in android?

One way to pass objects in Intents is for the object's class to implement Serializable. This interface doesn't require you to implement any methods; simply adding implements Serializable should be enough. To get the object back from the Intent, just call intent.


1 Answers

Try to pass the serializable list using Bundle.Serializable:

Bundle bundle = new Bundle(); bundle.putSerializable("value", all_thumbs); intent.putExtras(bundle); 

And in SomeClass Activity get it as:

Intent intent = this.getIntent(); Bundle bundle = intent.getExtras();  List<Thumbnail> thumbs=                (List<Thumbnail>)bundle.getSerializable("value"); 
like image 185
ρяσѕρєя K Avatar answered Oct 09 '22 15:10

ρяσѕρєя K