How to send object which contain Document and Map<String, Object> as fileds from one Activity to Another Activity?

35 views
Skip to first unread message

Nenad Stojnic

unread,
Jul 28, 2016, 11:15:51 AM7/28/16
to Couchbase Mobile
Hi.

Problem description:
Activity_A have one Meta object which contains two fields com.couchbase.lite.Document and Map<String, Object>.
We need to send Meta object from Activity_A to Activity_B and then we edit Meta object in Activity_B.
We want to do it using Intent.
Meta object must be Parcable.
How can we make that Meta Object is Parcable?

James Nocentini

unread,
Jul 28, 2016, 11:25:55 AM7/28/16
to Couchbase Mobile
You can pass the HashMap as an intent extra.

James

James Nocentini

unread,
Jul 28, 2016, 11:29:17 AM7/28/16
to Couchbase Mobile
But it looks like Maps can't be serialized as intent extras. The document.getProperties() method returns a Map<String, Object>. Can you try to convert it to a HashMap then pass it as an extra?

James  

Nenad Stojnic

unread,
Jul 28, 2016, 7:38:23 PM7/28/16
to Couchbase Mobile
Hi James.

We try to implement Parcelable interface. Something like this https://developer.android.com/reference/android/os/Parcelable.html.
Example on stackoverflow are all around but for maps we cant find anything. HashMap use Serializable.

With primitive types everything is great but when we try to use map type we are in problem.

How to use parcable with primitive types:
https://gist.github.com/keyboardr/5563038

And also how to put in bundle
https://gist.github.com/mmarcon/6660453

Our solution for now look something like this:
@Override
public void writeToParcel(Parcel dest, int flags) {
ParcelableUtils.write(dest, getFrequency());
ParcelableUtils.write(dest, getStartDate());
//dest.writeMap(properties);
}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public RecurringMetaEntity createFromParcel(Parcel in) {
return new RecurringMetaEntity(in);
}
public RecurringMetaEntity[] newArray(int size) {
return new RecurringMetaEntity[size];
}
};

private RecurringMetaEntity(Parcel in){
super(Enums.DatabaseType.ORG);
setType(DocumentTypeCode.RECURRING_META);
setFrequency(ParcelableUtils.readString(in));
setStartDate(ParcelableUtils.readDate(in));
}

As you can see we are using primitive types from our properties and then send object to another activity ... when we found better solution I will write in this thread.

If someone is interested to see how it really works you can try android application https://play.google.com/store/apps/details?id=net.dzomba.termin3

Hello to all good people!

Hod Greeley

unread,
Jul 29, 2016, 5:58:49 PM7/29/16
to Couchbase Mobile
An alternative approach is to use a static field to pass the data.  This SO post shows one of the better (IMHO) ways of implementing this.

I've seen other discussions about using weak reference maps to achieve this in a slightly more flexible way.

This is one aspect of Android I find frustrating.  Intents and Parcelables use Binder.  Binder passes data in and out of kernel memory.  Unnecessary copies are unpleasant.  Being limited by kernel memory space is unpleasant.

Using globals/singletons is pretty clearly not so great either.  It will make code harder to read and maintain.  It's a lot easier to write, and has much less overhead than Parcelables, though.

Hod
Reply all
Reply to author
Forward
0 new messages