Passing data through intent using Serializable
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");
This code may help you:
public class EN implements Serializable {//... you don't need implement any methods when you implements Serializable}
Putting data. Create new Activity with extra:
EN enumb = new EN();Intent intent = new Intent(getActivity(), NewActivity.class);intent.putExtra("en", enumb); //second param is SerializablestartActivity(intent);
Obtaining data from new activity:
public class NewActivity extends Activity { private EN en; @Override public void onCreate(Bundle savedInstanceState) { try { super.onCreate(savedInstanceState); Bundle extras = getIntent().getExtras(); if (extras != null) { en = (EN)getIntent().getSerializableExtra("en"); //Obtaining 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:
Bundle bundle = new Bundle();for (int i = 0; i<all_thumbs.size(); i++)bundle.putSerializable("extras"+i, all_thumbs.get(i));intent.putExtras(bundle);
In order to get the extras from the intent, you need:
Bundle bundle = new Bundle();List<Thumbnail> thumbnailObjects = new ArrayList<Thumbnail>();// collect your Thumbnail objectsfor (String key : bundle.keySet()) {thumbnailObjects.add((Thumbnail) bundle.getSerializable(key));}// for example, in order to get a value of the 3-rd object you need to:String label = thumbnailObjects.get(2).get_label();
Advantage of Serializable
is its simplicity. However, I would recommend you to consider using Parcelable
method when you need transfer many data, because Parcelable
is specifically designed for Android and it is more efficient than Serializable
. You can create Parcelable
class using:
- an online tool - parcelabler
- a plugin for Android Studion - Android Parcelable code generator