2

I wish to pass an arbitary object via an Android intent, I have the following code:

In the activity I wish to send an intent from:

Intent intent = new Intent(containerListActivity, com.uk.jacob.containerdroid.activities.ContainerDetailsActivity.class);
intent.putExtra("container", containers.get(i));
containerListActivity.startActivity(intent);

The activity I wish to recieve an intent:

System.out.println(getIntent().getExtras("container"));

I seem to get the error "cannot resolve method putExtra.

containers is essentially a Jackson JSON mapped POJO.

frogatto
  • 28,539
  • 11
  • 83
  • 129
  • check this post [pass custom objects between activites in android](http://stackoverflow.com/a/33073711/2032561) – Bharatesh Jan 17 '16 at 16:41
  • Possible duplicate of [Passing custom objects between activities?](http://stackoverflow.com/questions/5621132/passing-custom-objects-between-activities) – Bharatesh Jan 17 '16 at 16:42

2 Answers2

3

Unfortunately, you can't put an arbitrary object in the Intent. It needs to be Parcelable or Serializable.

http://developer.android.com/reference/android/content/Intent.html

You can do it a few ways:

  1. Have your custom class implement Serializable. This is quick to code, but may not be performant.
  2. Have your custom class implement Parcelable. This could be a bit of work.
  3. Convert your container to a string using Jackson, then convert it back after using getStringExtra.
frogatto
  • 28,539
  • 11
  • 83
  • 129
chessdork
  • 1,999
  • 1
  • 19
  • 20
1

Serialize your JSON to a string and then push it to the intent. In the receiving activity try getStringExtra() to extract it from the intent object.

frogatto
  • 28,539
  • 11
  • 83
  • 129