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.
You need to create a Bundle and then use putSerializable:
Sending Data:
First make your serializable data by implement
Serializable
to your data classThen put it into intent
Receiving Data:
I extended ρяσѕρєя K's answer to make the code full and workable. So, when you finish filling your 'all_thumbs' list, you should put its content one by one into the bundle and then into the intent:
In order to get the extras from the intent, you need:
Advantage of
Serializable
is its simplicity. However, I would recommend you to consider usingParcelable
method when you need transfer many data, becauseParcelable
is specifically designed for Android and it is more efficient thanSerializable
. You can createParcelable
class using:In SomeClass.java
This code may help you:
Putting data. Create new Activity with extra:
Obtaining data from new activity:
In kotlin: Object class implements Serializable:
At the point where the object sending:
At the fragment, where we want to get our object: